cmasupra
cmasupra

Reputation: 340

Understanding Tkinter Tk, Frame, and Toplevel object types (from a Java Swing perspective)

I'm having trouble understanding the types of different variables related to Frames and windows in Tkinter (in Python). Note that I come from the world of Java Swing (and some C# WinForms).

There are 3 main types in Tkinter that I'm aware of: Tk, Frame, and Toplevel. Right now, I think of the types this way:

With Tkinter, if I want to add a widget to a window, I add it to a Frame or a Toplevel, as shown below.

btnCreatePopup = tk.Button(<Frame or Toplevel instance>, text='Create popup', command=createPopup)

However, if I want to make a popup window modal, I don't use the same 2 types as I did for the widget, as shown below.

<Tk or Toplevel instance>.wm_attributes('-disabled', True)

I guess I'm confused because Toplevel objects accept both window attributes and widgets, but Tk objects only accept window attributes and Frame objects only accept widgets? Is my understanding of the 3 types in the bulleted list above correct and I am thinking about the types correctly? Do any of these 3 classes share a parent class that would help me understand the concept in Tkinter? I saw Wm (Window Manager) is a shared parent class of Tk and Toplevel, which tells me they are both windows, but I only add widgets to 1 type of Wm and not the other type of Wm?

Upvotes: 1

Views: 596

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386325

I hope this answer clears up some confusion, but I'm afraid it might make it more confusing. The problem in talking about this is that tkinter widgets don't really exist in a hierarchy like swing widgets do. In the underlying tk library, the hierarchy is essentially flat -- all widgets are peers of each other and none inherit from any others.

There are 3 main types in Tkinter that I'm aware of: Tk, Frame, and Toplevel

Three seems to be a reasonable number, but I think the three types are "window" (Tk and Toplevel), "widget" (Frame, Button, Entry, etc), and "menu" (the Menu widget).

There's nothing particularly special about a Frame widget. You can put it inside any other widget, and you can put almost any other widget inside a frame.

You shouldn't make another pair of Tk and Frame instances.

That is half correct. Under most circumstances you should only have a single instance of Tk even though more are technically possible (but with consequences that might not be obvious) However, you can create as many instances of Frame that you want. They are very useful for grouping widgets together when laying out your widgets inside a window.

With Tkinter, if I want to add a widget to a window, I add it to a Frame or a Toplevel

Technically, you can add almost any widget to almost any other widget, with the exception that you can't put widgets inside a Menu. You could put a frame inside a scrollbar if you wanted, or a canvas inside a text widget, or a frame inside a label. Of course, a lot of that won't make much sense from the user perspective. For the most part, other widgets are placed directly inside a Tk, Toplevel, or inside a Frame since all three are designed to be containers for other widgets. There are other widgets designed to be containers too, such as PanedWindow, LabelFrame, and the Notebook widget from the ttk module.

I guess I'm confused because Toplevel objects accept both window attributes and widgets, but Tk objects only accept window attributes and Frame objects only accept widgets?

Tk objects can have any other widget inside it, except for a Toplevel or another instance of Tk. It is quite normal and common to put text widgets, canvases, labels, buttons, and scrollbars directly inside a Tk or Toplevel window.

Do any of these 3 classes share a parent class that would help me understand the concept in Tkinter?

Not really. Technically, all tkinter widget classes except Tk inherit from Widget or BaseWidget, but the underlying tk widgets don't inherit from anything. Those base classes mostly supply some common functionality such as the pack, place, and grid methods.

Upvotes: 1

Related Questions