While I am not telling that your code has to resemble the code below, because the same behaviour can be written in some different ways and styles, it will do what you want.
I see two or three fundamental errors in your code.
You are passing the main root window as a parameter by calling Tk()
.
That is wrong because there should be only one Tk instance, made with a TK()
call in your tkinter program. Give it a name such as root and use that reference from them on.
Secondly, you don't see anything because you sleep forever without ever calling mainloop()
, which you should, otherwise your program will not update the UI, nor it will respond to events.
mainloop is the tkinter's eventloop for the Tk
instance. So setup your complete UI with all the widgets and make sure the code reaches and ending statement calling root.mainloop()
.
Also you usually don't need to call sleep()
and that is a blocking function. Any blocking function would also block the mainloop, inhibiting updates and event reception, until it returns.
Now follows some code
from tkinter import ttk, Tk, Toplevel
root = Tk()
welcome_window = Toplevel(root)
welcome_window.title('Welcome')
lab_window = Toplevel(root)
lab_window.title('Lab')
root.withdraw() # hide root window
lab_window.withdraw() # hide lab window
def goto_lab():
welcome_window.destroy()
lab_window.deiconify() # show lab window
button1 = ttk.Button(welcome_window, text='Close',\
command=goto_lab)
button1.pack(padx=100, pady=50)
button2 = ttk.Button(lab_window, text='Close',\
command=quit)
button2.pack(padx=100, pady=50)
root.mainloop()
About giving some structure to your GUI code, as you are starting to learn tkinter, take a look at this thread