Example Programs Using wxPython

A survival guide for the post-wx-prefixed world.

Author: Patrick K. O'Brien
Contact: pobrien@orbtech.com
Date: 2003-03-21
Revision: 1.1

Contents

Introduction

If something hits you on the head, don't run around screaming that the sky is falling. Instead, take a close look and see if it wasn't a "wx" prefix that hit you. Apparently, they're dropping off wxPython class names like flies dropping dead in the scorching heat of a summer's day.

Yes, the world is changing, and even our little wxPython world must change with it. Then again, I'm not fond of pesky summertime flies, and I'm not too upset that the "wx" prefixes are going to bite the dust. I think it's for the best. But, being the kind, considerate person that I am, I decided to write this guide to make the wx namespace transition easier for everyone, even Chicken Little.

Rather than simply tell you that everything will be okay, I decided to show you that everything will be okay. To do that, I've created a bunch of example programs using the new wx package. I hope you like them.

Basic

It doesn't get much simpler than this. Every wxPython program needs an application and a frame. To encourage good coding habits, I've split them into separate modules. They don't do much, but they're a good starting point.

I include a simple App class in the frame module because the PyCrust "wrapper" utility (wrap.py) only works with modules that contain an application class. So including a simple one in each of your frame modules allows you to use the PyCrust runtime wrapper and debug your frames independent of your full application.

Here is the module (frame.py) that defines the frame class:

#!/usr/bin/env python

"""Basic frame class, with App for testing."""

__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
__cvsid__ = "$Id$"
__revision__ = "$Revision$"[11:-2]

import wx

class Frame(wx.Frame):
    """Frame class."""

    def __init__(self, parent=None, id=-1, title='Title',
                 pos=wx.DefaultPosition, size=(400, 200)):
        """Create a Frame instance."""
        wx.Frame.__init__(self, parent, id, title, pos, size)

class App(wx.App):
    """Application class."""

    def OnInit(self):
        self.frame = Frame()
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

def main():
    app = App()
    app.MainLoop()

if __name__ == '__main__':
    main()

And here is the module (app.py) that defines the application class and imports the frame from frame.py:

#!/usr/bin/env python

"""Basic application class."""

__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
__cvsid__ = "$Id$"
__revision__ = "$Revision$"[11:-2]

import wx

from frame import Frame

class App(wx.App):
    """Application class."""

    def OnInit(self):
        self.frame = Frame()
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

def main():
    app = App()
    app.MainLoop()

if __name__ == '__main__':
    main()

Hello

This program displays an image file (wxPython.jpg) inside a frame sized to match the graphic.

screenshots/hello-win98.png

Running hello.py on Windows.

screenshots/hello-linux.png

Running hello.py on Linux.

screenshots/hello-mac.png

Running hello.py on Mac OS X.

Here is the source code for hello.py:

#!/usr/bin/env python

"""Hello, wxPython! program."""

__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
__cvsid__ = "$Id$"
__revision__ = "$Revision$"[11:-2]

import wx

class Frame(wx.Frame):
    """Frame class that displays an image."""

    def __init__(self, image, parent=None, id=-1,
                 pos=wx.DefaultPosition, title='Hello, wxPython!'):
        """Create a Frame instance and display image."""
        temp = image.ConvertToBitmap()
        size = temp.GetWidth(), temp.GetHeight()
        wx.Frame.__init__(self, parent, id, title, pos, size)
        self.bmp = wx.StaticBitmap(parent=self, id=-1, bitmap=temp)

class App(wx.App):
    """Application class."""

    def OnInit(self):
        wx.InitAllImageHandlers()
        image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)
        self.frame = Frame(image)
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

def main():
    app = App()
    app.MainLoop()

if __name__ == '__main__':
    main()