Overview of Python GUI development (Hello World)
Python is well suited for rapid development of cross-platform applications of all sorts, and that includes desktop GUI apps. There are choices to be made when starting to develop a GUI application in Python, however, and this article provides the information you need to set out on the right path. We will discuss what serious GUI toolkits are available for Python, their strengths and weaknesses, and provide a simple demo application for each one.
Demo “hello world” application overview
The demo application is simple: a window containing a combo box that presents the choices “hello”, “goodbye”, and “heyo”; a text box in which free text can be entered; and a button which, when clicked, prints a greeting assembled from the values of the combo box and text box — e.g. “Hello, world!” — to the console. Here is a mockup of the example app:
Note that all the toolkits discussed here lend themselves to a similar object-oriented approach to our simple application; it is an object that inherits from a top-level GUI object, such as an application, window, or frame. The application window is filled with a vertical layout widget which manages the arrangement of the control widgets. Other GUI toolkits for Python, such as the long-obsolete pyFLTK or the simplistic easygui, take different approaches, but the toolkits discussed here represent the mainstream of Python GUI library development.
I have attempted to provide idiomatic code for each toolkit, but I am not expert in a couple of them; please let me know in the comments if I’ve done anything that might be misleading to new developers, and I’ll do my best to update the examples.
Choosing a Python GUI Toolkit
There are several toolkits available for GUI programming in Python; several are good, none are perfect, and they are not equivalent; there are situations where one or more otherwise excellent toolkits will not be appropriate. But the choice is not difficult, and for many applications, any GUI toolkit you choose will work. In the toolkit sections below, I’ve tried to honestly lay out the drawbacks and advantages of each, to help you make an informed choice.
A caution: Much of the information available online — for example, in the Python wiki’s GUI article or the Python 2.7 FAQ — is out-of-date and misleading. Many of the toolkits on those pages have been unmaintained for five years or more, and others are too immature or undocumented for serious use. Also, do not use the Choose Your GUI Toolkit page I’ve seen bandied about here and there; its weighting code makes it almost impossible to choose anything but wxPython.
We will look at the four most popular Python GUI toolkits: TkInter, wxPython, pyGTK/PyGobject, and PyQt/PySide.
TkInter is the grand old man of the Python GUI world, having been distributed with Python almost since the language was founded. It is cross-platform and omnipresent, stable and reliable, and easy to learn, it has an object-oriented, brilliantly Pythonic API, and it works with all Python versions, but it has some drawbacks as well:
Limited support for theming
Before Python 2.7/3.1, TkInter had no support for theming, so on every platform its custom-drawn widgets looked like Motif circa 1985. Since, the ttk module has introduced themed widgets, which improve the look somewhat but leave some unorthodox controls, such as OptionMenus, in place.
Small selection of widgets
Tkinter lacks some widgets other toolkits provide, such as non-textual listboxes, real combo-boxes, scrolled windows, and others. Some additional controls have been supplied by third parties, e.g. Tix to supply the lack. Mock up your application in advance if you consider using TkInter, and make sure it can provide every control you need, or you could get burned later.
Demo App/Hello World in Tkinter
Note that at the time of writing, wxPython is only available for Python 2.x
Guido van Rossum, the creator of Python, says of wxPython:
wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn’t the standard Python GUI toolkit is that Tkinter was there first.
At least, he said that once, a long time ago; and how much GUI programming does Guido van Rossum do, anyway?
At any rate, wxPython does have a lot of good points. It has a vast set of widgets, native look-and-feel on Windows, Linux, and OS X, and a large-enough user base that at least the obvious bugs have been caught. It’s not included with Python, but installation is easy, and it’s available for all recent Python 2.x releases; wxPython is probably now the most popular GUI library for Python.
It’s not perfect, though; its documentation is atrocious, and though the exemplary demo app is priceless documentation itself, it’s about all the help you’re going to get. I personally find the API unPythonic and unpleasant, but others disagree. Still, you’ll almost never get in the middle of a project using wxPython and find it can’t do what you need, which is a big recommendation for it.
Demo App/Hello World in wxPython
# Every wx app must create one App object
# before it does anything else using wx.
# Set up the main window
# The greetings available
# Layout panel and hbox
# Greeting combobox
# Add the greeting combo to the hbox
# Recipient entry
# Add the greeting combo to the hbox
# Add padding to lower the button position
# The go button
# Bind an event for the button
# Make the button the default action for the form
# Add the button to the hbox
# Tell the panel to use the hbox
”’ Print a greeting constructed from
the selections made by the user. ”’
”’ Run the app ”’
# Instantiate and run