GUI programming with Python

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

Gui ToolkitWindowsUnix/X11MacOSNotes
TkinterYesYesYes, mostlyTkinter 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.
PyQtYesYesOS X onlyPyQt is based on Qt, the cross-platform GUI toolkit by Troll Tech. It's also, not so coincidentally, the subject of this book.
wxPythonYesYesNowxPython 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.
FxPyYesYesNoOne of the smaller - in terms of user base - toolkits, it is based on the FOX toolkit. FxPy's main feature is execution speed.
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.
PythonwinYesNoNoPythonwin 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: