[Tutor] Multiple windows in Tkinter

Alan Gauld alan.gauld at btinternet.com
Fri Oct 3 02:05:24 CEST 2008


"Glen Clark" <glenuk at gmail.com> wrote

> I am a little confused how multi-windows work in Tkinter. I am 
> currently
> using it while learning to program. Using it for single window 
> apps/scripts
> is fine. However what I dont understand is:
>
> What is toplevel? Is it the same as Tk()?

In any GUI thee is a containment heirarchy of widgets. tk() is the
highest level widget in any application. Top Level is the highest
level widget in a window which is separate from the main
application window. They differ in that you can kill a top level
window without killing the entire application but if you kill the
Tk based window everything should die!

> Do I initialise a root using Tk()

Yes. Tk is what runs the program. The mainloop() function
that organises the events is in Tk() as are several other application
evel methods.

> and then use toplevel for any other windows?

Exactly so.

> How do I switch between the Windows?

Usuially the user does that themselves although you can
do it programmatically by setting focus.

> And while I am on the subject what is a frame

A frame is just a container for other widgets, it helps you
organise your GUI and helps build reusable GUI components
by collecting all the bits onto a single Frame.

> and why should I use it?
> it just seems to be the same as Tk()?

No, they are completely different. A Frame does
not have a mainloop or quit function. And Tk does not
have the ability to set borders, colours etc. And you
cannot create reusable widget collectons using Tk
because you can only have one Tk in an appliation
but you can have many Frames.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list