Skip to content Skip to sidebar Skip to footer

Python - Tkinter - Widgets Created Inside A Class Inherited From Toplevel() Appear In A Different Frame OUTSIDE The Class, Toplevel() Class Is Empty

I'm attempting to create a class and inherit from Toplevel() so that the GUI elements of the class can be contained within a separate window. Usually I would just inherit from Fram

Solution 1:

When you do x=a().b(), what is stored in x is the result of b().

Consider the following line of code:

hostgameframe = Frame(self, bg='#999', bd=3, 
    relief=RIDGE, padx=5, pady=5).pack({"side": "left"})

If we collapse all of the options (for clarity) we're left with this:

hostgameframe = Frame(...).pack(...)

Can you see what's happening? hostgameframe is being set to the result of pack(...). pack always returns None, so hostgameframe is None. When you later create another widget and set this as it's master, that widget ends up going in the main window.

So, to solve your problem you need to separate the creation of the widget from the layout. Personally I think this is a best practice that you should always adhere to. For cases where you don't need to keep a reference to the widget, combining them into one statement is harmless. Even so, I think your code will be easier to manage if you get in the habit of always separating widget creation from widget layout.

hostgameframe = Frame(self, bg='#999', bd=3, relief=RIDGE, padx=5, pady=5)
hostgameframe.pack({"side": "left"})

Post a Comment for "Python - Tkinter - Widgets Created Inside A Class Inherited From Toplevel() Appear In A Different Frame OUTSIDE The Class, Toplevel() Class Is Empty"