Creating the bare minimum wxPython program

Let's begin with the simplest possible wxPython program that will run successfully. Create a file named "bare.py" and type in the following code. Remember, in Python, the spacing at the start of each line is significant.

import wx class App(wx.App):

def OnInit(self):

frame = wx.Frame(parent=None, title='Bare')

frame.Show()

return True app = App() app.MainLoop()

There's not much to it, is there? Even at only eight lines of code (not counting blank lines) this program might seem like a waste of space, as it does little more than display an empty frame. But bear with us, as we'll soon revise it, making it something more useful.

The real purpose of this program is to make sure you can create a Python source file, verify that wxPython is installed properly, and allow us to introduce more complex aspects of wxPython programming one step at a time. So humor us: create a file, type in the code, save the file with a name "bare.py," run it, and make sure it works for you.

The mechanism for running the program depends on your operating system. You can usually run this program by sending it as a command line argument to the Python interpreter from an operating system prompt, using one of the following commands:

python bare.py pythonw bare.py

Figures 1.5, 1.6, and 1.7 show what the program looks like running on various operating systems.

Figure 1.5

Running bare.py on Windows.

Figure 1.5

Running bare.py on Windows.

Figure 1.6

Running bare.py on Linux.

Figure 1.6

Running bare.py on Linux.

Figure 1.7

Running bare.py on Mac OS X.

Figure 1.7

Running bare.py on Mac OS X.

When most people look at this running program, they see something they would call a "window." However, wxPython does not call this a window. It calls this a "frame." In wxPython,"window" is the generic term for any object that displays on the screen (what other toolkits might call a "widget"). So, a wxPython programmer will often refer to objects such as buttons or text boxes as "windows." This may seem confusing, but the usage dates to the earliest days of the original C + + toolkit, and it's unlikely to change now. In this book, we'll try to avoid the use of window as a generic term, because it's confusing and also because it's the name of a big product from a major corporation. We'll use widget as the generic term. When we're specifically referring to the operating system of similar name, we'll do it with a capital "W."

While this bare-minimum program does little more than create and display an empty frame, all of its code is essential; remove any line of code and the program will not work. This basic wxPython program illustrates the five basic steps you must complete for every wxPython program you develop:

1 Import the necessary wxPython package

2 Subclass the wxPython application class

3 Define an application initialization method

4 Create an application class instance

5 Enter the application's main event loop

Let's examine this bare minimum program step-by-step to see how each one was accomplished.

Was this article helpful?

0 0

Post a comment