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.2 gives a sample of some issues that are difficult to manage in C + + but easy, if not trivial, in Python.

Table 1.2 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

Here is an example of how the two tools interact. In the previous section, we showed you a "hello world" example in C + + wxWidgets. Listing 1.5 shows the same example translated basically line-by-line into wxPython.

Listing 1.5 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):

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)

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_table macros, 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.

Was this article helpful?

0 0

Post a comment