dcsimg
December 3, 2016
Hot Topics:

Getting Started with wxPython

  • August 11, 2006
  • By Noel Rappin and Robin Dunn
  • Send Email »
  • More Articles »

We're going to start by working on a real wxPython program, albeit a simple one. We won't create anything complicated, yet. For now, we're going to lead you step-by-step through the process of creating your very first wxPython program. Let's begin by making sure you've got everything installed. Table 1 shows everything you'll need in order to run wxPython.

Table 1 Everything you'll need to run wxPython on your own computer

Tool Notes
The right operating system This is an easy one—you have a lot of options. Specifically, you must be running one of the following:
  • Any Microsoft Windows 32-bit operating system—meaning anything from Windows 98 onward (and you can get it going under Windows 95 if you must, but you'll need to download some extras).
  • Any Unix or Linux system capable of running the Gnome Toolkit (GTK).
  • A Macintosh running Mac OS X 10.2.3 or higher.
The Python programming language Available for download at www.python.org. Any version 2.3 or higher will work. Many Linux distributions include a version of Python, as does Mac OS X 10.3 or higher. Even so, you might still want to download the latest version.
The wxPython Toolkit

Available for download at www.wxpython.org.

There are different versions, depending on your operating system and Python version. Be sure to download the runtime installer that matches your platform, Python version, and Unicode preference. Download the packages for the demos and documentation as well.

If you've installed other software on your system, you should find that installing the wxPython packages works very similarly. Again, recent versions of Mac OS X and some Linux distributions already include wxPython, but you should download the latest version if you can.

A text editor

We recommend an editor that recognizes Python syntax and can do things like colorize the code to make it more readable. Most of the popular editors have support for Python code, so use the editor you prefer.

If you have no strong feelings for any particular editor, try IDLE, the integrated development environment included with Python, which includes a source code editor, interactive shell, debugger, and other tools.

The Python web site has a list of Python-aware editors at www.python.org/editors.

Once the installations are complete, get ready to type. We're going to create a program that displays a single image file. This will happen in three steps:

  1. We'll start with the bare minimum required for a working wxPython program.
  2. We'll make that code more structured and sophisticated.
  3. We'll end with a version that can display the wxPython logo.

Figures 1, 2, and 3, illustrate what the final program will look like, depending on your platform.

Figure 1 Running hello.py on Windows

Figure 2 Running hello.py on Linux

Figure 3 Running hello.py on Mac OS X

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 4, 5, and 6 show what the program looks like running on various operating systems.

Figure 4 Running bare.py on Windows.

Figure 5 Running bare.py on Linux.

Figure 6 Running bare.py on Mac OS X.

Jargon: It Looks Like a Window
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.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel