Apr-24-2024, 05:49 PM
(This post was last modified: Apr-24-2024, 05:49 PM by deanhystad.)
It doesn't matter if you call Toplevel(), Toplevel(None) or Toplevel(root). The default value for "master" is None. When master is none, the root window is assigned to be the window's master.
This is why one window programs can get away with not passing a master argument when making widgets. tk.Label(text="I am a label") adds a label to the root window, root being the default value for master. This should be avoided because it inevitably leads to confusion when you write a program with two windows or a custom dialog, and the widgets for your second window appear in the root. As seen here:
This is why one window programs can get away with not passing a master argument when making widgets. tk.Label(text="I am a label") adds a label to the root window, root being the default value for master. This should be avoided because it inevitably leads to confusion when you write a program with two windows or a custom dialog, and the widgets for your second window appear in the root. As seen here:
import tkinter as tk class BadWindow(tk.Toplevel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) tk.Label(text="I've been a very bad window!").pack() class GoodWindow(tk.Toplevel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) tk.Label(self, text="I follow the rules!").pack() root = tk.Tk() tk.Button(root, text="Make a good window.", command=GoodWindow).pack(padx=50, pady=10) tk.Button(root, text="Make a bad window.", command=BadWindow).pack(padx=50, pady=10) root.mainloop()When the program makes a BadWindow the label is added to the root window because no master is specified.