Using wxPython

We’ve always found that the best way to learn is by doing and then experimenting and tweaking with what’s been done. So download and install wxPython, fire up your favorite text editor[1] and get ready to play along as you read the next few sections.

A simple example

Familiarize yourself with this little wxPython program, and refer back to it as you read through the explanations that follow:

from wxPython.wx import *

class MyApp(wxApp):
    def OnInit(self):
        frame = wxFrame(NULL, -1, "Hello from wxPython")
        return true

app = MyApp(0)

When you run this code, you should see a Window appear similar to Figure 20.6.

A basic wxPython application
Figure 20.6. A basic wxPython application

The first thing to do is import the entire wxPython library with the from wxPython.wx import * statement. This is common practice for wxPython programs, but you can obviously perform more restrictive imports if you prefer.

Every wxPython application needs to derive a class from wxApp and provide an OnInit method for it. The framework calls this method as part of its initialization sequence, and the usual purpose of OnInit is to create the windows and essentials necessary for the program to begin operation. In the sample you created a frame with no parent, with a title of “Hello from wxPython" and then showed it. We could also have specified a position and size for the frame ...

Get Python Programming On Win32 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.