Skip to content Skip to sidebar Skip to footer

Tkinter - Open One Window And Close Another

I want to have a login screen and when the login is successful, the screen is closed and a new screen is created. The problem is , when I do just like the following code , both scr

Solution 1:

You probably don't want to have two Tk() instances running. For the login screen there's two routes you could go. You could withdraw the root window and make the login screen a Toplevel withdrawing the root window upon the Toplevel's initialization window and once the login is successful destroy the toplevel and raise the root window. Or, even easier you could put your login screen in a separate frame, hide the main frame using pack_forget or grid_forget depending on your layout and then login / destroy or hide the frame and recall pack or grid to show the main app frame again.


Solution 2:

Slayer , I did like you said and it worked like a charm! Here is the example code:

from tkinter import *

class Tela_login(Frame):
    def __init__(self,master):
        Frame.__init__(self, master)
        self.grid()
        self.button1 = Button(text = "Open",command = lambda: self.open_login())
        self.button1.grid()

    def open_login(self):
        root2 = Toplevel()
        app2 = Tela_principal(root2)

class Tela_principal(Frame):
    def __init__(self,master):
        Frame.__init__(self, master)
        self.grid

root = Tk()
root.geometry("800x600")
app = Tela_login(root)
root.mainloop()

Post a Comment for "Tkinter - Open One Window And Close Another"