Introduction to wxPython
by
Brent Woodruff
—
last modified
Jul 21, 2008 02:21 AM
Creating GUI Applications on Windows, Mac, and Linux
Also available in presentation modeā¦
Introduction
- Python - free, easy to use programming language
- wxWidgets - C++ GUI library
- wxPython - Python bindings to wxWidgets
- wxGlade - GUI builder for wxWidgets/wxPython
Getting Started
Install Python and wxPython on:
- Windows
- Mac (already installed with 10.5!)
- Linux
Anatomy of a GUI Application
- Windows
- Dialogs
- Widgets (buttons, etc)
- Event Loop
Minimal Application
import wx class Minimal(wx.App): def OnInit(self): main_window = wx.Frame(None, -1, "") self.SetTopWindow(main_window) main_window.Show() return 1 if __name__ == "__main__": app = Minimal(0) app.MainLoop()
Types of Windows
- wx.Frame
- wx.Panel
- wx.Dialog
- Other windows usually subclass or use one of these
Building up the GUI
- Application creates the top level window
- Top level window (wx.Frame) is the parent for:
- menu bar, status bar, panels, dialogs
- Panels and dialogs contain:
- buttons, text boxes, drop downs, etc
Building Menus
- Optionally, create new event IDs
- Create a menu bar
- Create a menu
- Append menu items to the menu
- Append the menu to the menu bar
- Set the frame's menu bar to the one created
- Bind the menu entry ID to an event handling method
Menu Code
class MinimalFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.main_window_menubar = wx.MenuBar()
self.SetMenuBar(self.main_window_menubar)
tmp_menu = wx.Menu()
tmp_menu.Append(wx.ID_EXIT, "Quit", "", wx.ITEM_NORMAL)
self.main_window_menubar.Append(tmp_menu, "File")
self.Bind(wx.EVT_MENU, self.OnQuit, id=wx.ID_EXIT)
def OnQuit(self, event): # wxGlade: LadFrame.
self.Close(True)
Adding a Panel for Widgets
Parent the panel to the creating frame.
self.panel = wx.Panel(self, -1)
Types of Widgets
- Button - wx.Button
- Toggle Button - wx.ToggleButton
- Text Box - wx.TextCtrl
- Static Text - wx.StaticText
- Drop Down Menu - wx.Choice, wx.ComboBox
- Selection List - wx.ListBox
- Radio button - wx.RadioButton, wx.RadioBox
- Checkbox - wx.CheckBox
- Slider - wx.Slider
Adding Widgets
As with the panel, parent widgets to the window containing them. In this case, the new panel.
self.btn = wx.Button(self.panel, -1, "My Button!") self.btn.Bind(wx.EVT_BUTTON, self.OnQuit)
Documentation
Sizers
Sizers control the placement of widgets, allowing them to grow and shrink depending on font sizes, languages, and window sizes.
- Sizers place widgets, they do not parent them
- Sizers lay items out horizontally, vertically, in grids, etc
- Sizers can be added to other sizers
- Sizers are the best and worst feature of wxWidgets/wxPython
- Using wxGlade can make using sizers easy, with preparation
wxGlade
- Go through "Building Up the GUI" - frame, panel, etc
- wxGlade adds sizers automatically where appropriate
- Use proportion and flags (wxEXPAND) on added items
- It takes time and experience to get used to effects
- Can generate C++, Python, Perl, XRC
- (let's build an app!)
Drawing
- wx.Panel
- wx.ScrolledWindow
- Draw Functions!
- Buffer with wx.BufferedDC or techniques
- LAD!

