WxPython for Everyone Charles Shapiro Atlanta Linux Enthusiasts May 2009
What Is WxPython? ● A cross-platform GUI development tool – Allows speedy development of GUI applications which will run on most popular platforms – Simple to understand and use – Actively developed with a large inventory of current projects – Open-source, L-GPL compatible license
wxWidgets Library ● C++ library designed for cross-platform widget development ● Julian Smart, University of Edinburgh, ● 22 pre-defined widgets (windows, panels, textboxes,sliders,buttons, et cetera) ● Lower-level API to develop your own custom widgets if necessary ● Other Wrappers: wxLua, wxPerl, WxBasic, D
WxPython Wrapper ● Python: Very Stylish – VM means no recompilation for different platforms – Access to Python libraries (e.g. DBI, pygame, et cetera) ● Must use Python 2.4 or better ● GPL-compatible license ● Full documentation
The Simplest Possible WxPython Program #!/usr/bin/env python import wx class myApp(wx.App): def OnInit(self): frame=wx.Frame(parent=None, title="Bare") frame.Show() return True app=myApp() app.MainLoop()
Demo wxtiny Here
A WxPython Program that Does Something
Demo example.py Here
wx.App() Object
wx.Frame Object
wxPython Main Line
Coding your WxPython GUI in XML ● Create an XRC file containing specs for all your widgets and windows. – Separation of Concerns ● Change your UI without editing your code – Use specialized editing tools to create and maintain most of your UI – XRC files are XML, so an XML parser is available in WxPython for other uses
Simple WxPython XRC Reader
Demo xrctest.py Here
Simple WxPython XRC File
Creating XRC files ● WxGlade – generates Python code as well ● XRCed
Demo wxGlade Here
Running on Windows ● All the usual fun: file types are by extension, script files won't work right, some tools may not exist. ● Initting an empty listbox in an XRC file (e.g. if you're filling it later from a database) causes an assertion exception in the windows wxwidgets C library. The cure is to put a dummy value in the listbox, remove it, and then fill it.
Other WxPython Features ● MDI (Multiple windows inside one, like OO 1.0) ● Validators (Hooks to manage your dialogs) ● Device Contexts (use WxPython for printing or drawing graphics in memory without display) ● Crude HTML display (lacks CSS et cetera)
WxPython Resources ● (Python) ● (wiki,listserve,doc) ● (wxwidgets) ● WxPython In Action (Noel Rappin, Robin Dunn; Mar 2006 Manning Publications, Greenwich CT)