Creating a GUI using Python, WxWidgets, and wxPython

After putting off this tutorial for as long as I possibly could (I’m not sure why) I have finally decided to buckle down and learn how to use WxWidgets using the WxPython Python bindings.

Installing

Getting WxWidgets and WxPython installed on my Debian Linux computer was as simple as installing the python-wxgtk2.6 Debian package. If you have any problems installing wxPython on your computer I suggest that you follow the instructions in the wxPython FAQ. Or you can follow the Getting Started instructions on the wxPython wiki.

Setting up the Window

Now that you have WxWidgets and WxPython installed it’s time to write a quick wxPython application. Start up your favourite python editor and enter the following:

#!/usr/bin/env python

import wx

if __name__ == "__main__":
	app = wx.App()
	app.MainLoop()

Save the file as wxHello.py, or whatever you would like. You can run the file if you want but you won’t actually see anything. The first thing that you will notice is that we import wx, this will import wxPython into your program.

The next thing that we do is create a wx.App and then call it’s MainLoop function, which starts the main GUI event loop. Here is a description of the wx.App class from the wxPython documentation :

The wx.App class represents the application and is used to:

  • bootstrap the wxPython system and initialize the underlying gui toolkit
  • set and get application-wide properties
  • implement the windowing system main message or event loop, and to dispatch events to window instances
  • etc.

Every application must have a wx.App instance, and all creation of UI objects should be delayed until after the wx.App object has been created in order to ensure that the gui platform and wxWidgets have been fully initialized.

Normally you would derive from this class and implement an OnInit method that creates a frame and then calls self.SetTopWindow(frame).

Notice the last little instruction there:

Normally you would derive from this class and implement an OnInit method that creates a frame and then calls self.SetTopWindow(frame).

So that is our next step, derive a class from wx.App and override the OnInit member:

#!/usr/bin/env python

import wx

class wxHelloApp(wx.App):
	"""The wx.App for the wxHello application"""

	def OnInit(self):
		"""Override OnInit to create our Frame"""
		frame = wx.Frame(None, title="wxHello")
		frame.Show()
		self.SetTopWindow(frame)
		return True

if __name__ == "__main__":
	app = wxHelloApp()
	app.MainLoop()

Now instead of creating a wx.App we create a wxHelloApp that is derived from a wx.App. All that the wxHelloApp does is override the OnInit() function. In that function it creates a wx.Frame (from the wxWidget docs):

A frame is a window whose size and position can (usually) be changed by the user. It usually has thick borders and a title bar, and can optionally contain a menu bar, toolbar and status bar. A frame can contain any window that is not a frame or dialog.

When we create the frame we set its parent to be nothing, and it’s title to be “wxHello”. If you run the wxHello.py file now you will be greeted by the following:

Python wxPython

Continue reading Creating a GUI using Python, WxWidgets, and wxPython