< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180025031194137176049106218111004229220065142056014176112

Tkinter and Python Programming

Tkinter Module: Adding Tk to your Applications

So what do you need to do to have Tkinter as part of your application? Well, first of all, it is not necessary to have an application already. You can create a pure graphical user interface if you want, but it probably isn't too useful without some underlying software that does something interesting.

There are basically five main steps that are required to get your GUI up-and-running:

  1. Import the Tkinter module (or from Tkinter import *).

  2. Create a top-level windowing object which contains your entire GUI application.

  3. Built all your GUI components (and functionality) on top (or "inside") of your top-level windowing object).

  4. Connect these GUI components to the underlying application code.

  5. Enter the main event loop.

The first step is trivial: All GUIs which use Tkinter must import the Tkinter module. Getting access to Tkinter is the first step (see the previous Section 18.1.2).

Introduction to GUI Programming

Before going to the examples, we will give you a brief introduction to GUI application development in general. This will give you some of the background you need to move forward.

Setting up a GUI application is similar to an artist's producing a painting. Conventionally, there is a single canvas onto which the artist must put all the work. The way it works is like this: You start with a clean slate, a "top-level" windowing object on which you build the rest of your components. Think of it as a foundation to a house or the easel for an artist. In other words, you have to pour the concrete or set up your easel before putting together the actual structure or canvas on top of it. In Tkinter, this foundation is known as the top-level window object.

In GUI programming, a top-level root windowing object contains all of the little windowing objects that will be part of your complete GUI application. These can be text labels, buttons, list boxes, etc. These individual little GUI components are known as widgets. So when we say create a top-level window, we just mean that you need such a thing as a place where you put all your widgets. In Python, this would typically look like the line below :

						
top = Tkinter.Tk() # or just Tk() with "from Tkinter import *"

					

The object returned by Tkinter.Tk() is usually referred to as the root object, hence the reason why some applications use root rather than top to indicate as such. Top-level windows are those which show up standalone as part of your application, and, yes, you may have more than one top-level window for your GUI, but only one of them should be your root window. You may choose to completely design all your widgets first, then add the real functionality, or do a little of this and a little of that along the way. (This means mixing and matching steps 3 and 4 from our list above.)

Widgets may be standalone or be containers. If a widget "contains" other widgets, it is considered the parent of those widgets. Accordingly, if a widget is "contained" in another widget, it's considered a child of the parent, the parent being the next immediate enclosing container widget.

Usually, widgets have some associated behaviors, such as when a button is pressed, or text is filled into a text field. These types of user behaviors are called events, and the actions that the GUI takes to respond to such events are known as callbacks.

Actions may include the actual button press (and release), mouse movement, hitting the RETURN or Enter key, etc. All of these are known to the system literally as events. The entire system of events which occurs from the beginning to the end of a GUI application is what drives it. This is known as event-driven processing.

One example of an event with a callback is a simple mouse move. Let's say the mouse pointer is sitting somewhere on top of your GUI application. If the mouse is moved to another part of your application, something has to cause the movement of the mouse on your screen so that it looks as if it is moving to another location. These are mouse move events that the system must process to give you the illusion (and reality) that your mouse is moving across the window. When you release the mouse, there are no more events to process, so everything just sits there quietly on the screen again.

The event-driven processing nature of GUIs fits right in with client-server architecture. When you start a GUI application, it must perform some setup procedures to prepare for the core execution, just as when a network server has to allocate a socket and bind it to a local address. The GUI application must establish all the GUI components, then draw (a.k.a. render or paint) them to the screen. Tk has a couple of geometry managers which help position the widget in the right place; the main one which you will use is called the packer. Once the packer has determined the sizes and alignments of all your widgets, it will then place them on the screen for you.

When all of the widgets, including the top-level window finally appear on your screen, your GUI application then enters a "server-like" infinite loop. This infinite loop involves waiting for a GUI event, processing it, then going back to wait for the next event.

The final step we described above says to enter the main loop once all the widgets are ready. This is the "server" infinite loop we have been referring to. In Tkinter, the code that does this is:

						
Tkinter.mainloop()

					

This is normally the last piece of sequential code your program runs. When the main loop is entered, the GUI takes over execution from there. All other action is via callbacks, even exiting your application. When you pull down the File menu to click on the Exit menu option or close the window directly, a callback must be invoked to end your GUI application.

Top-level window: Tkinter.Tk()

We mentioned above that all main widgets are built into the top-level window object. This object is created by the Tk class in Tkinter and is created via the normal instantiation:

						
>>> import Tkinter
>>> top = Tkinter.Tk()

					

Within this window, you place individual widgets or multiple-component pieces together to form your GUI. So what kinds of widgets are there? We will now introduce the Tk widgets.

Tk Widgets

There are currently 15 types of widgets in Tk. We present these widgets as well as a brief description in Table 18.1.

We won't go over the Tk widgets in detail as there is plenty of good documentation available on them, either from the Tkinter topics page at the main Python Web site or the abundant number of Tcl/Tk printed and online resources (some of which are available in the Appendix). However, we will present several simple examples to help you get started.

NOTE

GUI development really takes advantage of default arguments in Python because there are numerous default actions in Tkinter widgets. Unless you know every single option available to you for every single widget you are using, it's best to start out by setting only the parameters you are aware of and letting the system handle the rest. These defaults were chosen carefully. If you do not provide these values, do not worry about your applications appearing odd on the screen. They were created with an optimized set of default arguments as a general rule, and only when you know how to exactly customize your widgets should you use values other than the default.


Table 18.1. Tk Widgets
Widgets Description
Button similar to a Label but provides additional functionality for mouse overs, presses, and releases as well as keyboard activity/events
Canvas provides ability to draw shapes (lines, ovals, polygons, rectangles); can contain images or bitmaps
Checkbutton set of boxes of which any number can be "checked" (similar to HTML checkbox input)
Entry single-line text field with which to collect keyboard input (similar to HTML text input)
Frame pure container for other widgets
Label used to contain text or images
Listbox presents user list of choices to pick from
Menu actual list of choices "hanging" from a Menubutton that the user can choose from
Menubutton provides infrastructure to contain menus (pulldown, cascading, etc.)
Message similar to a Label, but displays multi-line text
Radiobutton set of buttons of which only one can be "pressed" (similar to HTML radio input)
Scale linear "slider" widget providing an exact value at current setting; with defined starting and ending values
Scrollbar provides scrolling functionality to supporting widgets, i.e., Text, Canvas, Listbox, and Entry
Text multi-line text field with which to collect (or display) text from user (similar to HTML textarea)
Toplevel similar to a Frame, but provides a separate window container


Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.