1

I created a library management system with GUI and want to add a login GUI on it as well. What I want in the login window to pop up first and then, if the credentials are right, the management system opens up.

The problem I'm facing is that when I hit the login button, the management system opens but the login window also stays there. I have tried self.root.destroy() before creating the window for management system but it closes all the windows and the management system also shuts down.`

What can I do?

class login_system():
    def __init__(self, root):
        self.root = root
        self.root.title('Login')
        self.root.geometry('500x250')
        self.root.config(background='black')
        self.loggedin = False

        user = Label(root, text='Username:', font=('times new roman',20, 'bold'), fg='white', bg='black', padx=15,pady=10)
        user.grid(row=0, column=0, padx=10, pady=10)

        password = Label(root, text='Password:', font=('times new roman',20, 'bold'), fg='white', bg='black', padx=15 )
        password.grid(row=1, column=0, padx=10, pady=10)

        self.user_var= StringVar()
        self.pass_var= StringVar()

        user_ent = Entry(root, width=20, font=('times new roman',18, 'bold'),textvariable=self.user_var)
        user_ent.grid(row=0, column=1)

        pass_ent = Entry(root, width=20,font=('times new roman',18, 'bold'),textvariable=self.pass_var)
        pass_ent.grid(row=1, column=1)

        submit = Button(root, text='Login', command=self.login,font=('times new roman',18, 'bold'))
        submit.grid(row=3, column=1, pady=10)

    def login(self):
        
        userinfo = self.user_var.get()
        passinfo= self.pass_var.get()
        conn = mysql.connector.connect(host='localhost', username='root', password = 'testpass', database = 'librarydb')
        my_cursor = conn.cursor()
        my_cursor.execute('SELECT username, password FROM login_system')
        rows = my_cursor.fetchall()

        conn.close()
        for row in rows:
            if row[0] ==userinfo:
                if row[1]==passinfo:
                    
                    tmsg.showinfo('Successful!', 'Logged In')
                    self.loggedin=True
                    self.newWindow= Toplevel(self.root)
                    
                    self.app = LibraryManagementSystem(self.newWindow)
                                                  
                    
                else:
                    tmsg.showinfo('Incorrect', 'Incorrect Password. Please try again')
                    break
            else:
                tmsg.showinfo('Incorrect', 'Incorrect username. Please try again')
                break
        

class LibraryManagementSystem():
    def __init__(self, root):
        self.root= root
        self.root.title('Library Management System')
        self.root.geometry('1366x768')
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • https://stackoverflow.com/questions/3819354/in-tkinter-is-there-any-way-to-make-a-widget-not-visible Have a look at this – RMRiver Jul 15 '21 at 15:11
  • That is because you are using TopLevel widgets here. So, when you close the main window, top levels close too. Make New window maybe – PCM Jul 15 '21 at 15:21

2 Answers2

0

Here, you can use .withdraw method. This will hide the main window without destroying the whole application. So, you can get your Toplevel() and hide the original window

self.root.withdraw()

This will hide the main window without destroying it.

And, after you are done, and you want to show the login window,

self.root.deiconify()

This will make to hidden window to be revealed.

0

When you destroy() the login window, all of the other windows are destroyed as well because it is the root window. To avoid this, the login window needs to be a Toplevel, not Tk.
You can create the root window like this:

root = Tk()
root.iconify()
login_system(root)

root.iconify() hides the root window, but it's still there so the other windows don't close. Then inside of the login class you need to create a Toplevel like this:

class login_system():
    def __init__(self, root):
        self.root = root
        self.login_window = Toplevel(self.root)
        self.login_window.title('Login')
        self.login_window.geometry('500x250')
        self.login_window.config(background='black')
        ...

This is similar to how you made the window for the managment system after the user has logged in.
After the new window is created, you need to destroy the login one like this:

self.newWindow= Toplevel(self.root)
                    
self.app = LibraryManagementSystem(self.newWindow)
self.login_window.destroy()    #This bit is new

Then it will all work as expected.

Henry
  • 3,472
  • 2
  • 12
  • 36
  • Thanks! It worked. Root with Tk() was being formed initially with the login system but then self.root.withdraw() worked there – Twarit Shah Jul 16 '21 at 19:12
  • @TwaritShah Glad I could help. Please mark this answer as accepted if you liked it. – Henry Jul 16 '21 at 19:27