An Introduction to Tkinter
  Copyright © 1997 by Fredrik Lundh <[email protected]>  
  Updated 9 Nov 1997  

< Events and Bindings | Application Windows | Standard Dialogues >

Application Windows

Coming soon.

Base Windows

In the simple examples we've used this far, there's only one window on the screen; the root window. This is automatically created when you call the Tk method, and is of course very convenient for simple applications:

from Tkinter import *

root = Tk()

create window contents as children to root

root.mainloop()

If you need to create additional windows, you can use the Toplevel widget. It simply creates a new window on the screen, a window that looks and behaves pretty much like the original root window:

from Tkinter import *

root = Tk()

create root window contents

top = Toplevel()

create top window contents

root.mainloop()

There's no need to use pack to display the Toplevel, it is automatically displayed by the window manager (in fact, you'll get an error message if you try to use pack or any other geometry manager with a Toplevel widget).

The Work Area

Menus

Toolbars

Status Bars

Most applications sport a status bar at the bottom of each application window. Implementing a status bar with Tkinter is trivial: you can simply use a suitably configured Label widget, and reconfigure the text option now and then. Here's one way to do it:

status = Label(master, text="", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)

The following class wraps a status bar label, providing set and clear methods:

Example: File: tkSimpleStatusBar.py

class StatusBar(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)
        self.label.pack(fill=X)

    def set(self, format, *args):
        self.label.config(text=format % args)
        self.label.update_idletasks()

    def clear(self):
        self.label.config(text="")
        self.label.update_idletasks()

Note that the set method works like C's printf function; it takes a format string, possibly followed by a set of arguments (a drawback is that if you wish to print an arbitrary string, you must do that as set("%s", string)). Also note that this method calls the update_idletasks method, to make sure pending draw operations (like the status bar update) are carried out immediately.

[For compatibility with future versions of Tkinter, we've decided that it is not politically correct to inherit from any widgets other than the Toplevel and Frame widgets. Should definitely explain why somewhere in this document.]

< Events and Bindings | Application Windows | Standard Dialogues >