wxpython overviewthis topic was written by robin dunn, author of the wxpython wrapper.
what is wxpython?
what is wxpython?wxpython is a blending of the wxwidgets gui classes and the python programming language. python so what is python? go to http://www.python.org to learn more, but in a nutshell python is an interpreted, interactive, object-oriented programming language. it is often compared to tcl, perl, scheme or java. python combines remarkable power with very clear syntax. it has modules, classes, exceptions, very high level dynamic data types, and dynamic typing. there are interfaces to many system calls and libraries, and new built-in modules are easily written in c or c++. python is also usable as an extension language for applications that need a programmable interface. python is copyrighted but freely usable and distributable, even for commercial use. wxpython wxpython is a python package that can be imported at runtime that includes a collection of python modules and an extension module (native code). it provides a series of python classes that mirror (or shadow) many of the wxwidgets gui classes. this extension module attempts to mirror the class hierarchy of wxwidgets as closely as possible. this means that there is a wxframe class in wxpython that looks, smells, tastes and acts almost the same as the wxframe class in the c++ version. wxpython is very versatile. it can be used to create standalone gui applications, or in situations where python is embedded in a c++ application as an internal scripting or macro language. currently wxpython is available for win32 platforms and the gtk toolkit (wxgtk) on most unix/x-windows platforms. see the wxpython website http://wxpython.org/ for details about getting wxpython working for you.
why use wxpython?so why would you want to use wxpython over just c++ and wxwidgets? personally i prefer using python for everything. i only use c++ when i absolutely have to eke more performance out of an algorithm, and even then i usually code it as an extension module and leave the majority of the program in python. another good thing to use wxpython for is quick prototyping of your wxwidgets apps. with c++ you have to continuously go though the edit-compile-link-run cycle, which can be quite time consuming. with python it is only an edit-run cycle. you can easily build an application in a few hours with python that would normally take a few days or longer with c++. converting a wxpython app to a c++/wxwidgets app should be a straight forward task.
other python guisthere are other gui solutions out there for python. tkinter tkinter is the de facto standard gui for python. it is available on nearly every platform that python and tcl/tk are. why tcl/tk? well because tkinter is just a wrapper around tcl's gui toolkit, tk. this has its upsides and its downsides... the upside is that tk is a pretty versatile toolkit. it can be made to do a lot of things in a lot of different environments. it is fairly easy to create new widgets and use them interchangeably in your programs. the downside is tcl. when using tkinter you actually have two separate language interpreters running, the python interpreter and the tcl interpreter for the gui. since the guts of tcl is mostly about string processing, it is fairly slow as well. (not too bad on a fast pentium ii, but you really notice the difference on slower machines.) it wasn't until the latest version of tcl/tk that native look and feel was possible on non-motif platforms. this is because tk usually implements its own widgets (controls) even when there are native controls available. tkinter is a pretty low-level toolkit. you have to do a lot of work (verbose program code) to do things that would be much simpler with a higher level of abstraction. pythonwin pythonwin is an add-on package for python for the win32 platform. it includes wrappers for mfc as well as much of the win32 api. because of its foundation, it is very familiar for programmers who have experience with mfc and the win32 api. it is obviously not compatible with other platforms and toolkits. pythonwin is organized as separate packages and modules so you can use the pieces you need without having to use the gui portions. others there are quite a few other gui modules available for python, some in active use, some that haven't been updated for ages. most are simple wrappers around some c or c++ toolkit or another, and most are not cross-platform compatible. see this link for a listing of a few of them.
using wxpythonfirst things first... i'm not going to try and teach the python language here. you can do that at the python tutorial. i'm also going to assume that you know a bit about wxwidgets already, enough to notice the similarities in the classes used. take a look at the following wxpython program. you can find a similar program in the wxpython/demo directory, named dialogunits.py. if your python and wxpython are properly installed, you should be able to run it by issuing this command:
001: ## import all of the wxpython gui package 002: from wxpython.wx import * 003: 004: ## create a new frame class, derived from the wxpython frame. 005: class myframe(wxframe): 006: 007: def __init__(self, parent, id, title): 008: # first, call the base class' __init__ method to create the frame 009: wxframe.__init__(self, parent, id, title, 010: wxpoint(100, 100), wxsize(160, 100)) 011: 012: # associate some events with methods of this class 013: evt_size(self, self.onsize) 014: evt_move(self, self.onmove) 015: 016: # add a panel and some controls to display the size and position 017: panel = wxpanel(self, -1) 018: wxstatictext(panel, -1, "size:", 019: wxdlg_pnt(panel, wxpoint(4, 4)), wxdefaultsize) 020: wxstatictext(panel, -1, "pos:", 021: wxdlg_pnt(panel, wxpoint(4, 14)), wxdefaultsize) 022: self.sizectrl = wxtextctrl(panel, -1, "", 023: wxdlg_pnt(panel, wxpoint(24, 4)), 024: wxdlg_sze(panel, wxsize(36, -1)), 025: wxte_readonly) 026: self.posctrl = wxtextctrl(panel, -1, "", 027: wxdlg_pnt(panel, wxpoint(24, 14)), 028: wxdlg_sze(panel, wxsize(36, -1)), 029: wxte_readonly) 030: 031: 032: # this method is called automatically when the close event is 033: # sent to this window 034: def onclosewindow(self, event): 035: # tell the window to kill itself 036: self.destroy() 037: 038: # this method is called by the system when the window is resized, 039: # because of the association above. 040: def onsize(self, event): 041: size = event.getsize() 042: self.sizectrl.setvalue("%s, %s" % (size.width, size.height)) 043: 044: # tell the event system to continue looking for an event handler, 045: # so the default handler will get called. 046: event.skip() 047: 048: # this method is called by the system when the window is moved, 049: # because of the association above. 050: def onmove(self, event): 051: pos = event.getposition() 052: self.posctrl.setvalue("%s, %s" % (pos.x, pos.y)) 053: 054: 055: # every wxwidgets application must have a class derived from wxapp 056: class myapp(wxapp): 057: 058: # wxwidgets calls this method to initialize the application 059: def oninit(self): 060: 061: # create an instance of our customized frame class 062: frame = myframe(null, -1, "this is a test") 063: frame.show(true) 064: 065: # tell wxwidgets that this is our main window 066: self.settopwindow(frame) 067: 068: # return a success flag 069: return true 070: 071: 072: app = myapp(0) # create an instance of the application class 073: app.mainloop() # tell it to start processing events 074: things to notice
wxwidgets classes implemented in wxpythonthe following classes are supported in wxpython. most provide nearly full implementations of the public interfaces specified in the c++ documentation, others are less so. they will all be brought as close as possible to the c++ spec over time.
where to go for helpsince wxpython is a blending of multiple technologies, help comes from multiple sources. see http://wxpython.org/ for details on various sources of help, but probably the best source is the wxpython-users mail list. you can view the archive or subscribe by going to http://lists.wxwindows.org/mailman/listinfo/wxpython-users or you can send mail directly to the list using this address: wxpython-users@lists.wxwindows.org
|