I l@ve RuBoard |
6.3 Tkinter OverviewOf all these GUI options, though, Tkinter is by far the de facto standard way to implement portable user interfaces in Python today, and the focus of this part of the book. Tkinter's portability, availability, accessibility, documentation, and extensions have made it the most widely used Python GUI solution for many years running. Here are some of the reasons why:
Naturally, other factors such as documentation and extensions are important when using a GUI toolkit too; let's take a quick look at the story Tkinter has to tell on these fronts as well. 6.3.1 Tkinter DocumentationThis book explores Tkinter fundamentals and most widgets tools, and should be enough to get started with substantial GUI development in Python. On the other hand, it is not an exhaustive reference to the Tkinter library. Happily, at least one book dedicated to using Tkinter in Python is now commercially available as I write this paragraph, and others are on the way (see the Python books list at http://www.python.org for details). Besides books, you can also now find Tkinter documentation online; a complete set of Tkinter manuals is currently maintained on the web at http://www.pythonware.com/library. In addition, because the underlying Tk toolkit used by Tkinter is also a de facto standard in the open source scripting community at large, other documentation sources apply. For instance, because Tk has also been adopted by the Tcl and Perl programming languages, Tk-oriented books and documentation written for both of these are directly applicable to Python/Tkinter as well (albeit, with some syntactic mapping). Frankly, I learned Tkinter by studying Tcl/Tk texts and references -- just replace Tcl strings with Python objects and you have additional reference libraries at your disposal (see Table 6-2, the Tk to Tkinter conversion guide, at the end of this chapter for help reading Tk documentation). For instance, the Tcl/Tk Pocket Reference (O'Reilly) can serve as a nice supplement to the Tkinter tutorial material in this part of the book. Moreover, since Tk concepts are familiar to a large body of programmers, Tk support is also readily available on the Net. 6.3.2 Tkinter ExtensionsBecause Tkinter is so widely used, programmers also have access to precoded Python extensions designed to work with or augment it. For instance:
If you plan on doing any commercial-grade GUI development with Tkinter, you'll probably want to explore extensions such as PMW and PIL after learning Tkinter basics in this text. They can save development time and add pizzazz to your GUIs. See the Python-related web sites mentioned earlier for up-to-date details and links. 6.3.3 Tkinter StructureFrom a more nuts-and-bolts perspective, Tkinter is an integration system that implies a somewhat unique program structure. We'll see what this means in terms of code in a moment, but here is a brief introduction to some of the terms and concepts at the core of Python GUI programming. Strictly speaking, Tkinter is simply the name of Python's interface to Tk -- a GUI library originally written for use with the Tcl programming language, and developed by Tcl's creator, John Ousterhout. Python's Tkinter module talks to Tk, and the Tk API in turn interfaces with the underlying window system: Microsoft Windows, X Windows on Unix, or Macintosh. Python's Tkinter adds a software layer on top of Tk that allows Python scripts to call out to Tk to build and configure interfaces, and routes control back to Python scripts that handle user-generated events (e.g., mouseclicks). That is, GUI calls are internally routed from Python script, to Tkinter, to Tk; GUI events are routed from Tk, to Tkinter, and back to a Python script. In Part V, we'll know these transfers by their C integration terms, extending and embedding.[5]
Luckily, Python programmers don't normally need to care about all this call routing going on internally; they simply make widgets and register Python functions to handle widget events. Because of the overall structure, though, event handlers are usually known as callback handlers, because the GUI library "calls back" to Python code when events occur. In fact, we'll find that Python/Tkinter programs are entirely event-driven: they build displays and register handlers for events, and then do nothing but wait for events to occur. During the wait, the Tk GUI library runs an event loop that watches for mouseclicks, keyboard presses, and so on. All application program processing happens in the registered callback handlers, in response to events. Further, any information needed across events must be stored in long-lived references like global variables and class instance attributes. The notion of a traditional linear program control-flow doesn't really apply in the GUI domain; you need to think in terms of smaller chunks. In Python, Tk also becomes object-oriented just because Python is object-oriented: the Tkinter layer exports Tk's API as Python classes. With Tkinter, we can either use a simple function-call approach to create widgets and interfaces, or apply OO techniques such as inheritance and composition to customize and extend the base set of Tkinter classes. Larger Tkinter GUIs generally are constructed as trees of linked Tkinter widget objects, and are often implemented as Python classes to provide structure and retain state information between events. As we'll see in this part of the book, a Tkinter GUI coded with classes almost by default becomes a reusable software component. |
I l@ve RuBoard |