One area where you do want the snappiest response possible is your user interface. Users are notoriously impatient creatures, and they are right. Responsiveness is important. Likewise, conformance to platform standards is important, as is a well thought-out programming model, to make your life easier. You want to have as little GUI code to as possible, because that means that there are less opportunities for bugs. With these criteria, we can set out to select a good GUI toolkit.
That it is possible at all to select a GUI toolkit might come as a bit of a surprise to Visual Basic developers, who cannot choose, but have to use whatever Microsoft provides. There is a cornucopia of GUI toolkits available for Unix/X11. Because Python is so easily extensible with C and C++ modules, a large part of them is usable, ‘bound to' is the technical term, from Python. Quite a few of those toolkits are available on Windows too. Because all computer intensive drawing and interaction code runs in native machine code, outside the Python virtual machine, the interface can be as responsive as the interface of an application written in C or C++.
The following GUI toolkits exist for Python:
Table 1-1. GUI Toolkits for Python
|Tkinter||Yes||Yes||Yes, mostly||Tkinter is the most ancient Python GUI toolkit. It is based on tcl/tk, and has neither the real platform UI look and feel, nor a real Python programming style. A good resource is John Grayson's book, Python and Tkinter programming.|
|PyQt||Yes||Yes||OS X only||PyQt is based on Qt, the cross-platform GUI toolkit by Troll Tech. It's also, not so coincidentally, the subject of this book.|
|wxPython||Yes||Yes||No||wxPython is based on the wxWindows toolkit. wxWindows is a crossplatform wrapper around a native toolkit of each platform: the standard Win32 controls on Windows and GTK on Unix/X11.|
|PyGTK (+PyGnome)||Yes (a bit)||Yes||(If you run a separate X Server on OS X)||PyGTK is based on GTK (formerly known as the Gimp Toolkit). Not really intended for cross-platform work, it has recently been ported (more or less) to Windows.|
|Pythonwin||Yes||No||No||Pythonwin is the - rather underdocumented - binding to Microsofts MFC library. It's not portable, of course.|
There are many others GUI toolkits available, both dead and alive. For a complete listing, please see Cameron Laird's notes on Python GUI's at: http://starbase.neosoft.com/~claird/comp.lang.python/python_GUI.html. However, the really serious options for someone selecting a toolkit are Tkinter, PyQt and wxPython. I have selected PyQt for my own use, based on criteria of performance, programming model, completeness of the assortment of widgets and ease of installation. Oh, and because it was the most fun to use, of course!
There were other considerations, of course. Tkinter is often very slow - try running the IDLE IDE that comes with Python. In contrast, PyQt is very snappy. The Tcl-tk programming model that Tkinter is based on doesn't translate as well to Python as the modified C++ programming model of PyQt. PyQt has also been very well designed: I just love the signal/slot mechanism of PyQt. There is also just about every type of widget I need, and PyQt is easy to install. WxPython, because it's a library (wxPython) based on a library (wxWindows) based on a library (MFC or GTK) can be really difficult to get up and running. Finally, the GUI designer in BlackAdder (or the free equivalent Qt Designer) is a strong point in favor of Qt, too.
The most important features of PyQt are:
Based on Trolltechs C++ Qt toolkit.
Runs on Windows and Unix/X11 (and soon on Apple's OS X)
Uses the innovative signals/slots paradigm to couple GUI items and actions.
Binds almost the complete Qt library
Allows subclassing of Qt classes in Python
Allows applications to mimic the look and feel of Windows, Motif, CDE, SGI and MacOS 9, or take on a custom look and feel.
Comes with an enormous inventory of advanced GUI controls, such as a canvas, an editable table module and a rich text editor (in version 3.0).