Tk
is the absolute root of the application, it is the first widget that needs to be instantiated and the GUI will shut down when it is destroyed.
Toplevel
is a window in the application, closing the window will destroy all children widgets placed on that window{1} but will not shut down the program.
try:
import tkinter as tk #python3
except ImportError:
import Tkinter as tk #python2
#root application, can only have one of these.
root = tk.Tk()
#put a label in the root to identify the window.
label1 = tk.Label(root, text="""this is root
closing this window will shut down app""")
label1.pack()
#you can make as many Toplevels as you like
extra_window = tk.Toplevel(root)
label2 = tk.Label(extra_window, text="""this is extra_window
closing this will not affect root""")
label2.pack()
root.mainloop()
If your python program only represents a single application (which it almost always will) then you should have only one Tk
instance, but you may create as many Toplevel
windows as you like.
try:
import tkinter as tk #python3
except ImportError:
import Tkinter as tk #python2
def generate_new_window():
window = tk.Toplevel()
label = tk.Label(window, text="a generic Toplevel window")
label.pack()
root = tk.Tk()
spawn_window_button = tk.Button(root,
text="make a new window!",
command=generate_new_window)
spawn_window_button.pack()
root.mainloop()
{1}: if a Toplevel (A = Toplevel(root)
) is the parent of another Toplevel (B = Toplevel(A)
) then closing window A will also close window B.