Scripting wxWindows with wxPython
By Robin Dunn
Robin runs the wxPython web site and can be contacted at [email protected].
C++ is not the only language where you can take advantage of wxWindows. wxPython is an Open Source Python extension module that wraps many of the GUI classes in wxWindows. In other words, the extension module lets you create C++ instances of wxWindows classes and invoke methods of those classes from Python (for more information about Python see http://www.python.org/). wxPython lets you create cross-platform GUI applications with a broad base of high-performance native components and high-level UI abstractions.
wxPython attempts to mirror the class hierarchy of wxWindows as closely as possible. This means that there is a wxFrame class in wxPython that looks, smells, tastes, and acts almost the same as the wxFrame class in the C++ version. This provides several benefits such as being able to use the same documentation for both languages and also makes it easy for you to use wxPython as a quick prototyping tool for applications that will later be converted to C++. Because of the differences in the languages not everything is used identically in Python, but the differences should be easy to absorb because they are usually natural to Python. For example, some methods that return multiple values via argument pointers in C++ will return a tuple of values in Python, or some methods that are overloaded in C++ are given different names in Python since there is no overloading. These differences are documented as a set of notes attached to the wxWindows documentation for the appropriate methods.
wxPython is currently in use in a wide variety of applications, everything from database front ends to scientific visualization applications. The wide variety of libraries and extension modules available for Python make it an ideal language for many kinds of applications, and with wxPython, adding a robust GUI is very easy. And it's about to get even easier. There is a project underway similar to Delphi called "Boa-Constructor" (http://boa-constructor.sourceforge.net/) that is implementing a GUI builder and an IDE for wxPython applications, and is itself implemented with wxPython. Don't let its low version number (currently 0.0.4) fool you; it is already quite advanced.
Listing Six is a simple wxPython application. When executed, this program creates a small frame window that contains a button labeled "Click Me," which, when clicked, will display a message dialog containing "Hello World." This example shows that using wxPython is similar to using wxWindows from C++. For example, to create a custom frame you simply derive a new class from the wxFrame class. To connect an event to an event handler you use a function for the event type (EVT_BUTTON in this case) that matches the event table macros used in C++. The example shows some "pythonic" things such as using a keyword argument, and using a tuple of values instead of a wxSize object when calling the wxFrame.__init__ method.
More information about wxPython, including downloadable sources and binaries for some platforms, is available at http://www.wxpython.org/.
DDJ