Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Programmer's Toolchest


May01: Scripting wxWindows with wxPython

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


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.