17.1. Tkinter Fundamentals
Tkinter makes it easy to build simple GUI applications. You import Tkinter, create, configure, and position the widgets you want, then enter the Tkinter main loop. Your application becomes event-driven: the user interacts with the widgets, causing events, and your application responds via the functions you installed as handlers for these events.
The following example shows a simple application that exhibits this general structure:
import sys, Tkinter Tkinter.Label(text="Welcome!").pack( ) Tkinter.Button(text="Exit", command=sys.exit).pack( ) Tkinter.mainloop( )
The calls to Label and Button create the respective widgets and return them as results. Since we specify no parent windows, Tkinter puts the widgets directly in the application's main window. The named arguments specify each widget's configuration. In this simple case, we don't need to bind variables to the widgets. We just call the pack method on each widget, handing control of the widget's layout to a layout manager object known as the packer. A layout manager is an invisible component whose job is to position widgets within other widgets (known as container or parent widgets), handling geometrical layout issues. The previous example passes no arguments to control the packer's operation, which lets the packer operate in a default way.
When the user clicks on the button, the command callable of the Button widget executes without arguments. The example passes function sys.exit as the argument named command when it creates the Button. Therefore, when the user clicks on the button, sys.exit( ) executes and terminates the application (as covered in exit on page 169).
After creating and packing the widgets, the example calls Tkinter's mainloop function, and thus enters the Tkinter main loop and becomes event-driven. Since the only event for which the example installs a handler is a click on the button, nothing happens from the application's viewpoint until the user clicks the button. Meanwhile, however, the Tkinter toolkit responds in the expected way to other user actions, such as moving the Tkinter window, covering and uncovering the window, and so on. When the user resizes the window, the packer layout manager works to update the widgets' layout. In this example, the widgets remain centered, close to the upper edge of the window, with the label above the button.
All strings going to or coming from Tkinter are Unicode strings, so be sure to review "Unicode" on page 198 if you need to show, or accept as input, characters outside of the ASCII encoding (you will then need to use the appropriate codec).
This chapter uses many all-uppercase, multiletter identifiers (e.g., LEFT, RAISED, and ACTIVE). All these identifiers are constant attributes of module Tkinter and are used for a wide variety of purposes. If your code uses from Tkinter import *, you can use the identifiers directly. If your code uses import Tkinter instead, you need to qualify these identifiers, just like all others you import from Tkinter, by preceding them with 'Tkinter.'. Tkinter is one of the rare Python modules designed to support from Tkinter import *, but using import Tkinter is still advisable, sacrificing some convenience in favor of greater clarity. A good compromise between convenience and clarity is to import Tkinter with a shorter name (e.g., import Tkinter as Tk).
Tkinter comes with several auxiliary modules to define dialogs, which are modal boxes that, when you activate them, return control to your application only when the user is done with them, either providing an answer you requested, or declining to answer by some button such as Cancel. These useful modules are not well documented and lack functionality your application may well need, such as internationalization (covered in "Internationalization" on page 269). For detailed understanding, as well as for adding such functionality, I suggest you examine the Python sources of these modules in Python's standard library: these sources are also good examples of how to use various aspects of Tkinter's functionality. In this section, I present only a general overview of the modules and some highlights of their most important functionality.
22.214.171.124. The tkMessageBox module
tkMessageBox supplies classes, functions, and constants that use Tk's "message boxes." You can specify the set of buttons to show, which one of the buttons you want to be the default, which icon to display, and title and message strings. Normally, you just call one of the following functions, with optional parameters title and message:
126.96.36.199. The tkSimpleDialog module
tkSimpleDialog supplies a Dialog class, which is a base class that you subclass to create your own custom dialogs, and three convenience functions, each with optional parameters title and prompt, and each returning a user response (when the user clicks OK) or None (when the user clicks Cancel):
188.8.131.52. The tkFileDialog module
tkFileDialog supplies classes and functions to let the user select a file or directory for loading (reading) or saving (writing). These classes and functions support many options, such as defaultextension to specify a default extension for files, filetypes to specify which extensions are accepted, and initialdir to specify where to start looking. The most commonly used utility functions in the module are:
184.108.40.206. The tkColorChooser module
tkColorChooser supplies a function askcolor, which you call without arguments, and returns a color chosen by the user, which is a tuple with four items: red, green, and blue components, followed by the Tkinter color string.