wxPython, a GUI Toolkit

You'll feel much better after reading about this new cross-platform toolkit written in Python and wrapped around wxWindows.

To many people in the “real world”, MS Windows is an inescapable, however unfortunate, fact of life. Some of these people might even have clients or bosses who require a “standard” Windows GUI application at the end of the day. For many developers, being able to choose the best tool for the task at hand, rather than being told which one to use, is an essential element of their quality of life, and Linux is a pleasant development environment.

It is in fact now possible to do quality, rapid GUI development for Windows on Linux, and is becoming increasingly possible for other GUI systems as well, such as Apple Macintosh, BeOS or OS/2. One of these tools, which I present in this article, is wxPython, which is not only based upon a proven toolkit, wxWindows, but uses our favorite language, Python.

Portable GUI Toolkits

There is no lack of choice of GUI development systems or toolkits under Linux, and quite a few of them can be called portable, i.e., work on at least two different platforms, for example, Linux/UNIX+X11 and Windows. Many of them are free, but not all of them are in usable form (i.e., documented, maintained, relatively bug-free and feature-rich). However, we are fortunate that such tools do exist. In fact, a well-maintained web page, http://www.free-soft.org/guitool/, lists an impressive number of such toolkits. The problem is deciding which one to choose, since life is too short to try them all.

How to Choose?

Life is a series of compromises. Often, the problem is finding out which set of drawbacks one is prepared to live with in return for which benefits. For one of my projects, I finally settled on the following list:

  • compatible with Linux, Windows and Python

  • usable

  • large enough widget set

  • not too slow

  • compact

  • easy to learn

  • converts to C/C++ easily after prototyping stage, if necessary

  • looks good

The last item was optional but “nice”. I found that wxPython fit the bill remarkably well, as it is based on GTK. If your list of constraints closely matches the above, you should give wxPython a look.

wxPython Qualities

At the time of this writing, wxPython is in release 2.1.13. Its author, Robin Dunn, has done a very good job of wrapping up wxWindows, one of the classic C++ GUI tool kits that has been around for several years. As a result of its relatively long existence and popularity, wxWindows has been ported to a number of toolkits and platforms and enjoys a relatively large widget collection. For Linux, the version of wxWindows that wxPython uses is wxGTK, the wxWindows interface to the GTK. For windows, the wxWindows port to win32 was used.

The interface of wxWindows to Python is remarkably transparent. The concepts are the same and the naming conventions are mostly the same. The object-oriented nature of the toolkit has been preserved. If you know how to program in C++ with wxWindows, you will have no problem with wxPython. Conversely, if you have a working proof of concept in wxPython, porting all or part of it to C++ for better speed should be straightforward.

wxPython is quite compact, but doesn't come with the standard Python distribution. See the Resources section on where to get the software. I found wxPython easy to learn; the distribution comes with a huge series of helpful demos which I will use in examples.

All of wxPython is open source, so people can contribute and fix bugs. In general, the software quality is high.

wxPython Drawbacks

For all its qualities, wxPython does have a few minor drawbacks. As of this writing, the wrapping of wxWindows is not complete, but Robin Dunn and others are working on finishing up.

The documentation is both very complete and almost non-existent. Robin can get away with this because wxPython is so similar to wxWindows that the wxWindows documentation, which is quite good and complete, can be used instead. The Python-specific part of the documentation is reduced to a small number of notes within the text (if a feature is not available or slightly different, for example) and a small section toward the end of the documentation. It is hardly any effort to do the mental translation from C++-type calls to Python, but if you've never seen C++ code in your life, this can be seriously off-putting.

The product is still very much in development. It follows wxGTK quite closely, but the wxGTK version it is based on is an unstable, development version. This means a number of things may not work perfectly, and things are likely to change a lot from version to version. I've had a lot of problems with printing under Linux, whereas it works perfectly under Windows.

wxPython would probably benefit greatly from a step-by-step tutorial. By the time you read this, someone will most likely have written one.

An interactive GUI-building tool would also be of interest. I understand that reusing the one from wxWindows should become possible soon.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

examples

Anonymous's picture

Nice examples, however how about removing the line numbers?

Try this

Mitch Frazier's picture

This should remove the lines numbers from the listings if you download them and save them to disk:

sed -e 's/^[0-9]* //' -i listing.py

Mitch Frazier is an Associate Editor for Linux Journal.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix