Main Content

wxPython - Introduction and sample

Archive - Originally posted on "The Horse's Mouth" - 2009-01-29 07:55:21 - Graham Ellis

Python has a number of GUIs ... and these days wxPython is the most popular. As one of the latter sessions of yesterday's Python Programming Course, I introduced the GUI and talked a little about how it's programmed, and the delegates put together a short sample application.

wxPython uses Python. So you need to know some Python. Then you import wx ... and write your application:

• Write a class (extending one of the ones that comes with wx) to define your components (widgets), how those components are laid out (geometry) and what to do when someone presses a button or drags the mouse (events).

• Create an object of that class, and put it in a frame

• Show the frame (important this one - otherwise you won't see your work)

• Call a main application loop method which waits for events and handles them. All the action of you code will take place within the event handler routines, and other code that those event handlers call - you won't call the functionality directly.

• That's the end of your code. The main loop never returns as you (should) have some sort of application exit within it, programmed onto a quit button.


Here's a sample display:


And the source code is here