[Tutor] Tkinter

Michael Lange klappnase at freenet.de
Sat Mar 6 19:54:51 EST 2004


On Sat, 6 Mar 2004 11:12:38 -0800 (PST)
Marilyn Davis <marilyn at deliberate.com> wrote:

> Hi again,
> 
> I'm a bit confused about the top-level processing in Tkinter.
> 
> 2 of my books start with tk=Tk() and build inside tk.
> 
> 1 book starts with o = MyClass() where MyClass(Frame) and
> builds inside itself.  But when I self.destoy() a frame gets
> left behind.
> 
> Then I read that there's a toplevel widget.  What's Tk()?
> 
> I know there's some understanding that I'm missing.
> 
> Can anyone help me?
> 
> Marilyn Davis
> 
Hi, Marilyn,

Tk() is the application's main (or root) window; any Tkinter app needs a Tk() window as parent for all the other widgets.
That's why you have to call the mainloop() for your Tk() window to keep the whole thing alive.
The toplevel widget (called with the Toplevel() command) is a window that looks just like the Tk() window,
but is in fact a child of the mainwindow. You don't need to call a mainloop() on Toplevel() windows.
When you close your mainwindow the children Toplevel windows are closed as well, but not vice versa.
Toplevel windows are used for things like dialog boxes or in complex guis where you need more than one window.

About the example with MyClass(Frame) I can just guess; maybe the code looked like this:

root = Tk()
o = MyClass(root)#Frame that contains some other widgets
o.pack()

Now when you destroy the MyClass instance an empty window should be left (the Tk() was not destroyed) which might look
like a window with a frame inside (an empty gray rectangle).

I hope this helps

Michael




More information about the Tutor mailing list