What Can wxPython Do?
Putting it together: the wxPython toolkit
While both Python and wxWidgets are pretty great on their own, they combine to create an even greater whole, like peanut butter and chocolate. The flexibility of the Python language makes wxPython much easier to develop in than its C++ counterpart, while the native C++ code of wxWidgets gives the Python GUI both the speed and native look and feel it would otherwise lack. Table 1 gives a sample of some issues that are difficult to manage in C++ but easy, if not trivial, in Python.
Table 1. Developing in C++ versus developing in wxPython
|C++ environment||wxPython environment|
|Memory management handled by programmer||Memory management handled by Python|
|Static typing makes polymorphism difficult||Dynamic typing makes polymorphism easy|
|Program reflection very limited||Program reflection easy, allowing for powerful abstraction|
|Unable to use functions as arguments easily||Functions can be passed around like any other variable|
|Compilation cycle needed before each run||Program interpreted at runtime|
Listing 2. A simple Hello World program in wxPython
import wx class MyApp(wx.App): def OnInit(self): frame = MyFrame("Hello World", (50, 60), (450, 340)) frame.Show() self.SetTopWindow(frame) return True class MyFrame(wx.Frame): def __init__(self, title, pos, size): wx.Frame.__init__(self, None, -1, title, pos, size) menuFile = wx.Menu() menuFile.Append(1, "&About...") menuFile.AppendSeparator() menuFile.Append(2, "E&xit") menuBar = wx.MenuBar() menuBar.Append(menuFile, "&File") self.SetMenuBar(menuBar) self.CreateStatusBar() self.SetStatusText("Welcome to wxPython!") self.Bind(wx.EVT_MENU, self.OnAbout, id=1) self.Bind(wx.EVT_MENU, self.OnQuit, id=2) def OnQuit(self, event): self.Close() def OnAbout(self, event): wx.MessageBox("This is a wxPython Hello world sample", "About Hello World", wx.OK | wx.ICON_INFORMATION, self) if __name__ == '__main__': app = MyApp(False) app.MainLoop()
There are two high-level things that we'd like to point out about the wxPython example compared to the wxWidgets C++ one (beyond merely the difference between the two languages).
First, notice that wxPython does not have the automatic macro for creating a main starting point, and must do so explicitly at the end of this module.
Second, the mechanism for associating events with the code to be executed is different between the two programs. Since Python allows functions to be passed easily around as objects, the wxPython program can use the relatively straightforward wx.Bind() methods to do the associating dynamically at runtime. The C++ program must use the DECLARE_EVENT_TABLE and BEGIN_EVENT_TABLEmacros, which do the binding statically at compile time and are somewhat more awkward.
Beyond those changes, the two programs are quite similar line by line-we find the Python version more readable, though. As you'll see, Python becomes more of an advantage in larger programs, due to its simpler syntax, automatic memory management, and so forth. At this point it's worth mentioning that wxPython did not come about by accident. It was developed to fill a specific need for a cross-platform rapid development environment. It has prospered and advanced because of the continued efforts of programmers who need rapid GUI development.
Development of wxPython and wxWidgets continues. Ongoing projects include support for mobile devices and better multimedia support. The most current version of wxPython is available at www.wxpython.org.
- You can create a minimal wxPython program in less than 10 lines of code. Most wxPython programs are much longer than 10 lines, and are typically divided into separate modules, each containing customized subclasses of wxPython classes, and, hopefully, plenty of docstrings.
- Most of the wxPython toolkit is accessed through the wx package which you access using the import wx statement. Every wxPython program must have an application object-an instance of a wx.App subclass that defines an OnInit() method. Most wxPython programs will have one or more frames- instances of subclasses of wx.Frame. A frame is the large, movable, resizeable window-like container that appears on screen, often with a menu, status bar, tool bars, and other widgets. Control of your program passes to wxPython when you call your application's MainLoop()method.
- Within wxPython are all the basic widgets you would expect, plus common dialogs, a wide variety of more complex widgets, HTML rendering, spreadsheet- style grids, and so forth. The wxWidgets toolkit that wxPython is based on is a C++ framework with a large list of features. It is a cross-platform toolkit, with most support for Microsoft Windows, Unix GTK+, and the Mac OS. The basic unit of a wxWidgets application is the window, meaning any item that can be drawn to the screen.
- The wxPython toolkit is a combination of the Python programming language and the wxWidgets toolkit and can be downloaded at www. wxpython.org. It combines a very extensive interface toolkit with an easy-touse scripting language. It offers productivity gains and useful features for any programmer, including existing Python or wxWidgets programmers.
- The wxPython version of the toolkit is a wrapper around wxWidgets containing bindings which allow Python language constructs to interact with the C++ framework. These bindings are largely created from the SWIG tool, from a long list of descriptions of how Python objects and C++ objects relate to each other.
About the Authors
Noel Rappinis a senior software engineer at a large technology company, and has extensive Python experience. He has a Ph.D. from the Georgia Institute of Technology, where he studied educational software and user interface design. Noel co-authored the book Jython Essentials.
Robin Dunn, the creator and maintainer of wxPython, has been working in the software industry for 18 years on a wide variety of applications. He discovered both wxWindows and Python in 1995 while looking for a cross platform toolkit and has never (willingly) looked back. Robin was awarded the ActiveState Programmers? Choice Award at the 2002 O?Reilly Open Source Convention. Robin also worked for the Open Source Applications Foundation, improving wxPython for use in their flagship product, Chandler.
Source of This Material
wxPython in Action
By Noel Rappin and Robin Dunn
Page 3 of 3