1 \chapter{wxPython Notes
}\label{wxPython
}
2 \pagenumbering{arabic
}%
3 \setheader{{\it CHAPTER
\thechapter}}{}{}{}{}{{\it CHAPTER
\thechapter}}%
4 \setfooter{\thepage}{}{}{}{}{\thepage}%
6 This addendum is written by Robin Dunn, author of the wxPython wrapper
8 %----------------------------------------------------------------------
9 \section{What is wxPython?
}\label{wxpwhat
}
11 wxPython is a blending of the wxWindows GUI classes and the
12 \urlref{Python
}{http://www.python.org/
} programming language.
16 So what is Python? Go to
17 \urlref{http://www.python.org
}{http://www.python.org
}
18 to learn more, but in a nutshell Python is an interpreted,
19 interactive, object-oriented programming language. It is often
20 compared to Tcl, Perl, Scheme or Java.
22 Python combines remarkable power with very clear syntax. It has
23 modules, classes, exceptions, very high level dynamic data types, and
24 dynamic typing. There are interfaces to many system calls and
25 libraries, and new built-in modules are easily written in C or
26 C++. Python is also usable as an extension language for applications
27 that need a programmable interface.
29 Python is copyrighted but freely usable and distributable, even for
34 wxPython is a Python package that can be imported at runtime that
35 includes a collection of Python modules and an extension module
36 (native code). It provides a series of Python classes that mirror (or
37 shadow) many of the wxWindows GUI classes. This extension module
38 attempts to mirror the class heiarchy of wxWindows as closely as
39 possble. This means that there is a wxFrame class in wxPython that
40 looks, smells, tastes and acts almost the same as the wxFrame class in
43 wxPython is very versitile. It can be used to create standalone GUI
44 applications, or in situations where Python is embedded in a C++
45 application as an internal scripting or macro language.
47 Currently wxPython is available for Win32 platforms and the GTK
48 toolkit (wxGTK) on most Unix/X-windows platforms. The effort to
49 enable wxPython for wxMotif will begin shortly. See
\helpref{Building Python
}{wxpbuild
} for
50 details about getting wxPython working for you.
53 %----------------------------------------------------------------------
54 \section{Why use wxPython?
}\label{wxpwhy
}
56 So why would you want to use wxPython over just C++ and wxWindows?
57 Personally I prefer using Python for everything. I only use C++ when
58 I absolutely have to eek more performance out of an algorithm, and even
59 then I ususally code it as an extension module and leave the majority
60 of the program in Python.
62 Another good thing to use wxPython for is quick prototyping of your
63 wxWindows apps. With C++ you have to continuously go though the
64 edit-compile-link-run cycle, which can be quite time comsuming. With
65 Python it is only an edit-run cycle. You can easily build an
66 application in a few hours with Python that would normally take a few
67 days or longer with C++. Converting a wxPython app to a C++/wxWindows app
68 should be a straight forward task.
70 %----------------------------------------------------------------------
71 \section{Other Python GUIs
}\label{wxpother
}
73 There are other GUI solutions out there for Python.
77 Tkinter is the defacto standard GUI for Python. It is available
78 on nearly every platform that Python and Tcl/TK are. Why Tcl/Tk?
79 Well because Tkinter is just a wrapper around Tcl's GUI toolkit, Tk.
80 This has its upsides and its downsides...
82 The upside is that Tk is a pretty veristile toolkit. It can be made
83 to do a lot of things in a lot of different environments. It is fairly
84 easy to create new widgets and use them interchangably in your
87 The downside is Tcl. When using Tkinter you actually have two
88 separate language interpreters running, the Python interpreter and the
89 Tcl interpreter for the GUI. Since the guts of Tcl is mostly about
90 string processing, it is fairly slow as well. (Not too bad on a fast
91 Pentium II, but you really notice the difference on slower machines.)
93 It wasn't until the lastest version of Tcl/Tk that native Look and
94 Feel's were possible on non-Motif platforms. This is because Tk
95 usually implements it's own widgets (controls) even when there are
96 native controls available.
98 Tkinter is a pretty low-level toolkit. You have to do a lot of work
99 (verbose program code) to do things that would be much simpler with a higher
100 level of abstraction.
102 \wxheading{PythonWin
}
104 PythonWin is an add-on package for Python for the Win32 platform. It
105 includes wrappers for MFC as well as much of the win32 API. Because
106 of its foundation, it is very familiar for programmers who have
107 experience with MFC and the Win32 API. It is obviously not compatible
108 with other platforms and toolkits. PythonWin is organized as separate
109 packages and modules so you can use the pieces you need without having
110 to use the GUI portions.
114 There are quite a few other GUI modules available for Python, some in
115 active use, some that havn't been updated for ages. Most are simple
116 wrappers around some C or C++ toolkit or another, and most are not
117 cross-platform compatible. See
\urlref{this
118 link
}{http://www.python.org/download/Contributed.html\#Graphics
}
119 for a listing of a few of them.
121 %----------------------------------------------------------------------
122 \section{Building wxPython
}\label{wxpbuild
}
124 I used SWIG (
\urlref{http://www.swig.org
}{http://www.swig.org
}) to
125 to create the source code for the
126 extension module. This enabled me to only have to deal with a small
127 amount of code and only have to bother with the exceptional issues.
128 SWIG takes care of the rest and generates all the repetative code for
129 me. You don't need SWIG to build the extension module as all the
130 generated C++ code is included under the src directory.
132 I added a few minor features to SWIG to control some of the code
133 generation. If you want to play around with this you will need to get
134 a recent version of SWIG from their CVS or from a daily build. See
135 \urlref{http://www.swig.org/
}{http://www.swig.org/
} for details.
137 wxPython is organized as a Python package. This means that the
138 directory containing the results of the build process should be a
139 subdirectory of a directory on the
\tt{PYTHONPATH
}. (And preferably should
140 be named wxPython.) You can control where the build process will dump
141 wxPython by setting the
\tt{TARGETDIR
} variable for the build utility, (see
144 \begin{enumerate
}\itemsep=
0pt
145 \item Build wxWindows as described in its BuildCVS.txt file. For *nix
146 systems I run configure with these flags:
157 --disable-std_iostreams
160 You can use whatever flags you want, but I know these work.
162 For Win32 systems I use Visual C++
6.0, but
5.0 should work also. The
163 build utility currently does not support any other win32 compilers.
165 \item At this point you may want to make an alias or symlink, script,
166 batch file, whatever on the PATH that invokes
167 \tt{\$(WXWIN)/utils/wxPython/distrib/build.py
} to help simplify matters
168 somewhat. For example, on my win32 system I have a file named
169 \tt{build
}.bat in a directory on the PATH that contains:
171 \tt{python \%WXWIN/utils/wxPython/distrib/build.py \%
1 \%
2 \%
3 \%
4 \%
5 \%
6}
174 \item Change into the
\tt{\$(WXWIN)/utils/wxPython/src
} directory.
176 \item Type "
\tt{build -b
}" to build wxPython and "
\tt{build -i
}" to
177 install it, or \"
\tt{build -bi
}\" to do both steps at once.
179 The build.py script actually generates a Makefile based on what it
180 finds on your system and information found in the build.cfg file.
181 If you have troubles building or you want it built or installed in
182 a different way, take a look at the docstring in build.py. You are
183 able to to override many configuration options in a file named
186 \item To build and install the add-on modules, change to the appropriate
187 directory under
\tt{\$(WXWIN)/utils/wxPython/modules
} and run the build
190 \item Change to the
\tt{\$(WXWIN)/utils/wxPython/demo
} directory.
192 \item Try executing the demo program. For example:
196 To run it without requiring a console on win32, you can use the
197 \tt{pythonw.exe
} version of Python either from the command line or from a
205 %----------------------------------------------------------------------
206 \section{Using wxPython
}\label{wxpusing
}
208 \wxheading{First things first...
}
210 I'm not going to try and teach the Python language here. You can do
211 that at the
\urlref{Python Tutorial
}{http://www.python.org/doc/tut/tut.html
}.
212 I'm also going to assume that you know a bit about wxWindows already,
213 enough to notice the similarities in the classes used.
215 Take a look at the following wxPython program. You can find a similar
216 program in the
\tt{wxPython/demo
} directory, named
\tt{DialogUnits.py
}. If your
217 Python and wxPython are properly installed, you should be able to run
218 it by issuing this command:
220 \begin{indented
}{1cm
}
221 \bftt{python DialogUnits.py
}
227 001: ## import all of the wxPython GUI package
228 002: from wxPython.wx import *
230 004: ## Create a new frame class, derived from the wxPython Frame.
231 005: class MyFrame(wxFrame):
233 007: def __init__(self, parent, id, title):
234 008: # First, call the base class' __init__ method to create the frame
235 009: wxFrame.__init__(self, parent, id, title,
236 010: wxPoint(
100,
100), wxSize(
160,
100))
238 012: # Associate some events with methods of this class
239 013: EVT_SIZE(self, self.OnSize)
240 014: EVT_MOVE(self, self.OnMove)
242 016: # Add a panel and some controls to display the size and position
243 017: panel = wxPanel(self, -
1)
244 018: wxStaticText(panel, -
1, "Size:",
245 019: wxDLG_PNT(panel, wxPoint(
4,
4)), wxDefaultSize)
246 020: wxStaticText(panel, -
1, "Pos:",
247 021: wxDLG_PNT(panel, wxPoint(
4,
14)), wxDefaultSize)
248 022: self.sizeCtrl = wxTextCtrl(panel, -
1, "",
249 023: wxDLG_PNT(panel, wxPoint(
24,
4)),
250 024: wxDLG_SZE(panel, wxSize(
36, -
1)),
252 026: self.posCtrl = wxTextCtrl(panel, -
1, "",
253 027: wxDLG_PNT(panel, wxPoint(
24,
14)),
254 028: wxDLG_SZE(panel, wxSize(
36, -
1)),
258 032: # This method is called automatically when the CLOSE event is
259 033: # sent to this window
260 034: def OnCloseWindow(self, event):
261 035: # tell the window to kill itself
264 038: # This method is called by the system when the window is resized,
265 039: # because of the association above.
266 040: def OnSize(self, event):
267 041: size = event.GetSize()
268 042: self.sizeCtrl.SetValue("
%s, %s" % (size.width, size.height))
270 044: # tell the event system to continue looking for an event handler,
271 045: # so the default handler will get called.
274 048: # This method is called by the system when the window is moved,
275 049: # because of the association above.
276 050: def OnMove(self, event):
277 051: pos = event.GetPosition()
278 052: self.posCtrl.SetValue("
%s, %s" % (pos.x, pos.y))
281 055: # Every wxWindows application must have a class derived from wxApp
282 056: class MyApp(wxApp):
284 058: # wxWindows calls this method to initialize the application
285 059: def OnInit(self):
287 061: # Create an instance of our customized Frame class
288 062: frame = MyFrame(NULL, -
1, "This is a test")
289 063: frame.Show(true)
291 065: # Tell wxWindows that this is our main window
292 066: self.SetTopWindow(frame)
294 068: # Return a success flag
298 072: app = MyApp(
0) # Create an instance of the application class
299 073: app.MainLoop() # Tell it to start processing events
304 \wxheading{Things to notice
}
306 \begin{enumerate
}\itemsep=
0pt
307 \item At line
2 the wxPython classes, constants, and etc. are imported
308 into the current module's namespace. If you prefer to reduce
309 namespace pollution you can use "
\tt{from wxPython import wx
}" and
310 then access all the wxPython identifiers through the wx module, for
311 example, "
\tt{wx.wxFrame
}".
312 \item At line
13 the frame's sizing and moving events are connected to
313 methods of the class. These helper functions are intended to be like
314 the event table macros that wxWindows employs. But since static event
315 tables are impossible with wxPython, we use helpers that are named the
316 same to dynamically build the table. The only real difference is
317 that the first arguemnt to the event helpers is always the window that
318 the event table entry should be added to.
319 \item Notice the use of
\tt{wxDLG
\_PNT} and
\tt{wxDLG
\_SZE} in lines
19
320 -
29 to convert from dialog units to pixels. These helpers are unique
321 to wxPython since Python can't do method overloading like C++.
322 \item There is an
\tt{OnCloseWindow
} method at line
34 but no call to
323 EVT
\_CLOSE to attach the event to the method. Does it really get
324 called? The answer is, yes it does. This is because many of the
325 \em{standard
} events are attached to windows that have the associated
326 \em{standard
} method names. I have tried to follow the lead of the
327 C++ classes in this area to determine what is
\em{standard
} but since
328 that changes from time to time I can make no guarentees, nor will it
329 be fully documented. When in doubt, use an EVT
\_*** function.
330 \item At lines
17 to
21 notice that there are no saved references to
331 the panel or the static text items that are created. Those of you
332 who know Python might be wondering what happens when Python deletes
333 these objects when they go out of scope. Do they disappear from the GUI? They
334 don't. Remember that in wxPython the Python objects are just shadows of the
335 coresponding C++ objects. Once the C++ windows and controls are
336 attached to their parents, the parents manage them and delete them
337 when necessary. For this reason, most wxPython objects do not need to
338 have a
\_\_del\_\_ method that explicitly causes the C++ object to be
339 deleted. If you ever have the need to forcibly delete a window, use
340 the Destroy() method as shown on line
36.
341 \item Just like wxWindows in C++, wxPython apps need to create a class
342 derived from
\tt{wxApp
} (line
56) that implements a method named
343 \tt{OnInit
}, (line
59.) This method should create the application's
344 main window (line
62) and use
\tt{wxApp.SetTopWindow()
} (line
66) to
345 inform wxWindows about it.
346 \item And finally, at line
72 an instance of the application class is
347 created. At this point wxPython finishes initializing itself, and calls
348 the
\tt{OnInit
} method to get things started. (The zero parameter here is
349 a flag for functionality that isn't quite implemented yet. Just
350 ignore it for now.) The call to
\tt{MainLoop
} at line
73 starts the event
351 loop which continues until the application terminates or all the top
352 level windows are closed.
355 %----------------------------------------------------------------------
356 \section{wxWindows classes implemented in wxPython
}\label{wxpclasses
}
358 The following classes are supported in wxPython. Most provide nearly
359 full implementations of the public interfaces specified in the C++
360 documentation, others are less so. They will all be brought as close
361 as possible to the C++ spec over time.
363 \begin{itemize
}\itemsep=
0pt
364 \item \helpref{wxAcceleratorEntry
}{wxacceleratorentry
}
365 \item \helpref{wxAcceleratorTable
}{wxacceleratortable
}
366 \item \helpref{wxActivateEvent
}{wxactivateevent
}
367 \item \helpref{wxBitmapButton
}{wxbitmapbutton
}
368 \item \helpref{wxBitmap
}{wxbitmap
}
370 \item \helpref{wxBrush
}{wxbrush
}
371 \item \helpref{wxButton
}{wxbutton
}
372 \item \helpref{wxCalculateLayoutEvent
}{wxcalculatelayoutevent
}
373 \item \helpref{wxCheckBox
}{wxcheckbox
}
374 \item \helpref{wxCheckListBox
}{wxchecklistbox
}
375 \item \helpref{wxChoice
}{wxchoice
}
376 \item \helpref{wxClientDC
}{wxclientdc
}
377 \item \helpref{wxCloseEvent
}{wxcloseevent
}
378 \item \helpref{wxColourData
}{wxcolourdata
}
379 \item \helpref{wxColourDialog
}{wxcolourdialog
}
380 \item \helpref{wxColour
}{wxcolour
}
381 \item \helpref{wxComboBox
}{wxcombobox
}
382 \item \helpref{wxCommandEvent
}{wxcommandevent
}
383 \item \helpref{wxConfig
}{wxconfigbase
}
384 \item \helpref{wxControl
}{wxcontrol
}
385 \item \helpref{wxCursor
}{wxcursor
}
386 \item \helpref{wxDC
}{wxdc
}
387 \item \helpref{wxDialog
}{wxdialog
}
388 \item \helpref{wxDirDialog
}{wxdirdialog
}
389 \item \helpref{wxDropFilesEvent
}{wxdropfilesevent
}
390 \item \helpref{wxEraseEvent
}{wxeraseevent
}
391 \item \helpref{wxEvent
}{wxevent
}
392 \item \helpref{wxEvtHandler
}{wxevthandler
}
393 \item \helpref{wxFileDialog
}{wxfiledialog
}
394 \item \helpref{wxFocusEvent
}{wxfocusevent
}
395 \item \helpref{wxFontData
}{wxfontdata
}
396 \item \helpref{wxFontDialog
}{wxfontdialog
}
397 \item \helpref{wxFont
}{wxfont
}
398 \item \helpref{wxFrame
}{wxframe
}
399 \item \helpref{wxGauge
}{wxgauge
}
404 \item \helpref{wxGrid
}{wxgrid
}
406 \item \helpref{wxIcon
}{wxicon
}
407 \item \helpref{wxIdleEvent
}{wxidleevent
}
408 \item \helpref{wxImage
}{wximage
}
409 \item \helpref{wxImageHandler
}{wximagehandler
}
410 \item \helpref{wxImageList
}{wximagelist
}
411 \item \helpref{wxIndividualLayoutConstraint
}{wxindividuallayoutconstraint
}
412 \item \helpref{wxInitDialogEvent
}{wxinitdialogevent
}
413 \item \helpref{wxJoystickEvent
}{wxjoystickevent
}
415 \item \helpref{wxKeyEvent
}{wxkeyevent
}
416 \item \helpref{wxLayoutAlgorithm
}{wxlayoutalgorithm
}
417 \item \helpref{wxLayoutConstraints
}{wxlayoutconstraints
}
418 \item \helpref{wxListBox
}{wxlistbox
}
419 \item \helpref{wxListCtrl
}{wxlistctrl
}
420 \item \helpref{wxListEvent
}{wxlistevent
}
421 \item \helpref{wxListItem
}{wxlistctrlsetitem
}
422 \item \helpref{wxMDIChildFrame
}{wxmdichildframe
}
423 \item \helpref{wxMDIClientWindow
}{wxmdiclientwindow
}
424 \item \helpref{wxMDIParentFrame
}{wxmdiparentframe
}
425 \item \helpref{wxMask
}{wxmask
}
426 \item wxMaximizeEvent
427 \item \helpref{wxMemoryDC
}{wxmemorydc
}
428 \item \helpref{wxMenuBar
}{wxmenubar
}
429 \item \helpref{wxMenuEvent
}{wxmenuevent
}
430 \item \helpref{wxMenuItem
}{wxmenuitem
}
431 \item \helpref{wxMenu
}{wxmenu
}
432 \item \helpref{wxMessageDialog
}{wxmessagedialog
}
433 \item \helpref{wxMetaFileDC
}{wxmetafiledc
}
434 \item \helpref{wxMiniFrame
}{wxminiframe
}
435 \item \helpref{wxMouseEvent
}{wxmouseevent
}
436 \item \helpref{wxMoveEvent
}{wxmoveevent
}
437 \item \helpref{wxNotebookEvent
}{wxnotebookevent
}
438 \item \helpref{wxNotebook
}{wxnotebook
}
439 \item \helpref{wxPageSetupDialogData
}{wxpagesetupdialogdata
}
440 \item \helpref{wxPageSetupDialog
}{wxpagesetupdialog
}
441 \item \helpref{wxPaintDC
}{wxpaintdc
}
442 \item \helpref{wxPaintEvent
}{wxpaintevent
}
443 \item \helpref{wxPalette
}{wxpalette
}
444 \item \helpref{wxPanel
}{wxpanel
}
445 \item \helpref{wxPen
}{wxpen
}
447 \item \helpref{wxPoint
}{wxpoint
}
448 \item \helpref{wxPostScriptDC
}{wxpostscriptdc
}
449 \item \helpref{wxPreviewFrame
}{wxpreviewframe
}
450 \item \helpref{wxPrintData
}{wxprintdata
}
451 \item \helpref{wxPrintDialogData
}{wxprintdialogdata
}
452 \item \helpref{wxPrintDialog
}{wxprintdialog
}
453 \item \helpref{wxPrinter
}{wxprinter
}
454 \item \helpref{wxPrintPreview
}{wxprintpreview
}
455 \item \helpref{wxPrinterDC
}{wxprinterdc
}
456 \item \helpref{wxPrintout
}{wxprintout
}
457 \item \helpref{wxQueryLayoutInfoEvent
}{wxquerylayoutinfoevent
}
458 \item \helpref{wxRadioBox
}{wxradiobox
}
459 \item \helpref{wxRadioButton
}{wxradiobutton
}
460 \item \helpref{wxRealPoint
}{wxrealpoint
}
461 \item \helpref{wxRect
}{wxrect
}
462 \item \helpref{wxRegionIterator
}{wxregioniterator
}
463 \item \helpref{wxRegion
}{wxregion
}
464 \item \helpref{wxSashEvent
}{wxsashevent
}
465 \item \helpref{wxSashLayoutWindow
}{wxsashlayoutwindow
}
466 \item \helpref{wxSashWindow
}{wxsashwindow
}
467 \item \helpref{wxScreenDC
}{wxscreendc
}
468 \item \helpref{wxScrollBar
}{wxscrollbar
}
469 \item \helpref{wxScrollEvent
}{wxscrollevent
}
470 \item \helpref{wxScrolledWindow
}{wxscrolledwindow
}
472 \item \helpref{wxSingleChoiceDialog
}{wxsinglechoicedialog
}
473 \item \helpref{wxSizeEvent
}{wxsizeevent
}
474 \item \helpref{wxSize
}{wxsize
}
475 \item \helpref{wxSlider
}{wxslider
}
476 \item \helpref{wxSpinButton
}{wxspinbutton
}
478 \item \helpref{wxSplitterWindow
}{wxsplitterwindow
}
479 \item \helpref{wxStaticBitmap
}{wxstaticbitmap
}
480 \item \helpref{wxStaticBox
}{wxstaticbox
}
482 \item \helpref{wxStaticText
}{wxstatictext
}
483 \item \helpref{wxStatusBar
}{wxstatusbar
}
484 \item \helpref{wxSysColourChangedEvent
}{wxsyscolourchangedevent
}
485 \item \helpref{wxTaskBarIcon
}{wxtaskbaricon
}
486 \item \helpref{wxTextCtrl
}{wxtextctrl
}
487 \item \helpref{wxTextEntryDialog
}{wxtextentrydialog
}
488 \item \helpref{wxTimer
}{wxtimer
}
490 \item \helpref{wxToolBar
}{wxtoolbar
}
492 \item \helpref{wxTreeCtrl
}{wxtreectrl
}
493 \item \helpref{wxTreeEvent
}{wxtreeevent
}
494 \item \helpref{wxTreeItemData
}{wxtreeitemdata
}
496 \item \helpref{wxUpdateUIEvent
}{wxupdateuievent
}
497 \item \helpref{wxWindowDC
}{wxwindowdc
}
498 \item \helpref{wxWindow
}{wxwindow
}
503 %----------------------------------------------------------------------
504 \section{Where to go for help
}\label{wxphelp
}
506 Since wxPython is a blending of multiple technologies, help comes from
507 multiple sources. See
508 \urlref{http://alldunn.com/wxPython
}{http://alldunn.com/wxPython
} for details on
509 various sources of help, but probably the best source is the
510 wxPython-users mail list. You can view the archive or subscribe by
513 \urlref{http://starship.python.net/mailman/listinfo/wxpython-users
}{http://starship.python.net/mailman/listinfo/wxpython-users
}
515 Or you can send mail directly to the list using this address:
517 wxpython-users@starship.python.net