October 24, 2016
Hot Topics:

Getting Started with wxPython

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

Creating the final hello.py program

Now that you've got the basics under your belt, let's create the final version of the program we showed at the beginning of this chapter. Create a file named hello.py and enter in the code shown in listing 2.

Listing 2 The final hello.py

Click here for a larger image.

Developer.com Note:The numbers in the following list indicate the "callout" numbers in the listing above.
  1. The shebang line allows this program to be an executable script under Linux and other Unix-like operating systems.
  2. Defining a custom Frame class that subclasses wx.Frame lets us more easily control the Frame's contents and appearance.
  3. We added an image parameter to our Frame's constructor. This value is provided by our application class when it creates a Frame instance. As long as we can pass the required values to wx.Frame.__init__(), there's no reason we can't add more parameters to our subclass's constructor.
  4. We're going to display the image in a wx.StaticBitmap control, which requires a bitmap. So we convert the image to a bitmap. We also create a size tuple, using the width and height of the bitmap. The size tuple is supplied to the wx.Frame.__init__() call, so that the frame size matches the bitmap size.
  5. Defining a wx.App subclass with an OnInit() method is a minimum requirement for any wxPython application.
  6. We create an image object, using a wxPython.jpg file stored in the same directory as hello.py. You can get this file from the Manning web site, or substitute one of your own. A more sophisticated version of this program would accept the name of a file from the command line. We pass our image object as a parameter when we create the frame.
  7. The main() function creates an application instance and starts the wxPython event loop.
  8. Checking if this module is the main module allows it to be used in two different ways: run from the command line or imported by another module.

What happened when you ran your version of hello.py? Did you see a frame sized to match the graphic you provided? If not, brush yourself off and try again. If so, congratulations! You're ready to move on to the next exciting steps.

But before you rush into the next article, we're going to talk about wxPython a little more broadly, what it's capable of, and how it came to be. If that doesn't interest you, feel free to jump to the next chapter and continue coding—the rest of the introduction will still be here.

About the Authors

Noel Rappin is 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

Published: March 2006, 620 pages
Published by Manning Publications
ISBN: 1932394621
PDF ebook: $25.00
Softbound book + PDF ebook : $49.95

Page 3 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