wxPython is a GUI toolkit for Python. Over 230 classes from wxWindows are supported, so it is more complex then Tk or GTK+, but not nearly as bloated as Swing.

The developers created it using SWIG, so polymorphism suffered a little and it's possible to create memory leaks.

The following example tries to illustrate object creation, window layout and event handling:

  from wxPython import wx
  # Creating an application object initialized the library

  # Most constructors expect a parent object as the first parameter
  # and an id number as second parameter.
  dialog = wx.wxDialog(None, -1, "Title")
  button = wx.wxButton(dialog, wx.wxID_OK, "Hello world")

  # A simple way to describe a window-layout is to use sizers.
  # Add a five pixel border to ALL sides and allow the button to
  # EXPAND in all directions.
  sizer = wx.wxBoxSizer(wx.wxHORIZONTAL)
  sizer.Add(button, 1, wx.wxEXPAND|wx.wxALL, 5)
  dialog.Fit()    # Make the dialog as small as possible.
  dialog.Layout() # Calculate the position of the button.

  def our_event_handler(event):
    event.Skip() # Try to call another handler.

  # Tell the dialog to call our_event_handler if the button is pressed.
  wx.EVT_BUTTON(dialog, button.GetId(), our_event_handler)

  dialog.Destroy()   # Yes, you have to destroy dialogs explicitly. :-(

wxPython is a binding of the wxWindows cross-platform GUI toolkit to the Python language. It is a mature and well-supported toolkit that is probably the best overall widget toolkit for Python. It is available everywhere wxWindows is, which at the moment is under x11, Windows, Mac OS, Mac OS X, and soon OS/2. On X11 it can bind to Motif or GTK+, or experimentally it can use its own widgets. The most well-known wxPython application at the moment is BitTorrent.

Using wxPython

The classes for the wxPython toolkit are found in the wxPython.wx module, and thus one of the first lines in any wxPython-based program is from wxPython.wx import *. Despite the effective Python module namespacing system, all wxPython class names begin with the letters 'wx'. Every wxPython application must instantiate a wxApp object to run the main event loop. Since the wxApp class does not contain any initialisation code, every program must subclass wxApp and add an OnInit method, or use a pre-made wxApp derivative like wxPySimpleApp.

Widgets in wxPython, as in any other object-oriented widget library, are treated as class instances. The most important widget class in wxPython is probably wxFrame, which is the widget for a toplevel window. Other important classes are wxButton, wxMenu, wxPanel, and wxDialog.

An annotated wxPython Hello World program is:

#!/usr/bin/env python

from wxPython.wx import * # Need this to use wxPython

class hwApp(wxApp):
    def OnInit(self):  # Initialisation code for the app
        main = wxFrame(None, -1, "Hello World")  # Top level window
        button = wxButton(main, 1, 'Quit')  # Creates a 'Quit' button with ID 1
        EVT_BUTTON(button, 1, lambda event: main.Close()) # Maps the button-press to an event handler 

                                                          # (if this were a real app it'd be a real function)

        main.Show()  # make the items appear
        return True

hwApp(0).MainLoop()  # Actually run the program

More complicated window layout is usually implemented using 'sizers', which arrange widgets in groups in very flexible ways. Widgets in sizers can be set to maintain arbitrary size ratios under window resizing.

A simple wxPython tutorial can be found at http://www.wxpython.org/tutorial.php, and a more comprehensive overview at http://wiki.wxpython.org/index.cgi/Getting_20Started

Comparison to other toolkits

The three main competitors to wxPython are Tkinter, PyGTK, and PyQt. Tkinter has the advantage of being the default toolkit in the Python distribution, and of working the most uniformly across platforms, but is disadvantaged by its comparative lack of features and rough-cut appearance on some platforms. PyQt is not free for Win32, limiting its usefulness as a cross-platform toolkit.

The main competition to wxPython is thus PyGTK. Both are based on modern, C or C++-based toolkits, and in fact on X11 wxPython uses GTK+ as a backend. An advantage of wxPython on non-X11 platforms is that it binds directly to native widgets, where PyGTK draws its own widgets in many cases. This has an effect on both the look and feel of the application. wxPython is also an older and thus more mature toolkit, especially on non-X11 platforms where GTK+ is still fairly new. In addition, on X11 wxPython uses version 1 of the GTK+ library while PyGTK uses the cleaner version 2, though this is a relatively minor point for those not using the library directly.


wxPython is a full-featured GUI toolkit for the Python language. At the moment, it is the most mature toolkit other than the default but rather limited Tkinter. It is fairly easy to learn and is available on all major desktop platforms. At the moment it is the best toolkit available for cross-platform development, although PyGTK is improving quite rapidly along with GTK+ support for non-X11 platforms.

This writeup is copyright 2003 D.G. Roberge and is released under the Creative Commons Attribution-NonCommercial-ShareAlike licence. Details can be found at http://creativecommons.org/licenses/by-nc-sa/2.0/ .

Log in or register to write something here or to contact authors.