Inheritance from Tkinter Frame in varying implementations Inheritance from Tkinter Frame in varying implementations tkinter tkinter

Inheritance from Tkinter Frame in varying implementations


Because MainWindow inherits from Frame, it is itself a frame. If you never call pack, place, or grid on it, it will be invisible. This is no different than if you created a button or scrollbar or any other widget and then don't call one of those methods on it.

Since all of the other widgets are a children of this frame, they will be invisible since their parent is invisible.


Somewhat unrelated to the question being asked, self.pack() is a code smell. Generally speaking, a class should never call pack, place or grid on itself. This tightly couples itself to the caller (meaning, this widget has to know that the caller is using one of those methods).

In other words, if you decide that MainApplication wants to switch from pack to grid for all of its children, you can't just update MainApplication, you also have to update MainWindow. In this case, the root window has only one child so the problem is fairly small, but by doing it this way you are starting a bad practice that will eventually cause you problems.

The rule of thumb is that the function that creates a widget should be responsible for adding it to the screen. That means that it would be better to do it like this:

root = tk.Tk() my_gui = myGUI.MainApplication(root)my_gui.pack(fill="both", expand=True)

You would then need to remove self.pack() from MainApplication.__init__.

You also have some very misleading code that might be contributing to the confusion. Take a look at this code:

# Main Data Windowclass MainWindow(tk.Frame):     def __init__(self, master):         ...         self.main_window = tk.Label(self, text="This is a test")         self.main_window.pack(side="top", fill="x")

Notice how you have a class named MainWindow. Within that you have a label that is named self.main_window, but self.main_window is not a MainWindow. This is especially confusing since the function that creates MainWindow also creates an instance variable named self.main_window.

You might want to consider renaming this label to be something else (eg: self.label, self.greeting, etc).