]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/wxPython.tex
This form of the event cloning patch survived my
[wxWidgets.git] / docs / latex / wx / wxPython.tex
1 \chapter{wxPython Notes}\label{wxPython}
2
3 \setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
4 \setfooter{\thepage}{}{}{}{}{\thepage}%
5
6 This addendum is written by Robin Dunn, author of the wxPython wrapper
7
8 %----------------------------------------------------------------------
9 \section{What is wxPython?}\label{wxpwhat}
10
11 wxPython is a blending of the wxWindows GUI classes and the
12 \urlref{Python}{http://www.python.org/} programming language.
13
14 \wxheading{Python}
15
16 So what is Python? Go to
17 \urlref{http://www.python.org}{http://www.python.org} to learn more,
18 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.
21
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.
28
29 Python is copyrighted but freely usable and distributable, even for
30 commercial use.
31
32 \wxheading{wxPython}
33
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 heirarchy of wxWindows as closely as
39 possible. 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
41 the C++ version.
42
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.
46
47 Currently wxPython is available for Win32 platforms and the GTK
48 toolkit (wxGTK) on most Unix/X-windows platforms. See the wxPython
49 website \urlref{http://wxPython.org/}{http://wxPython.org/} for
50 details about getting wxPython working for you.
51
52 %----------------------------------------------------------------------
53 \section{Why use wxPython?}\label{wxpwhy}
54
55 So why would you want to use wxPython over just C++ and wxWindows?
56 Personally I prefer using Python for everything. I only use C++ when I
57 absolutely have to eek more performance out of an algorithm, and even
58 then I usually code it as an extension module and leave the majority
59 of the program in Python.
60
61 Another good thing to use wxPython for is quick prototyping of your
62 wxWindows apps. With C++ you have to continuously go though the
63 edit-compile-link-run cycle, which can be quite time consuming. With
64 Python it is only an edit-run cycle. You can easily build an
65 application in a few hours with Python that would normally take a few
66 days or longer with C++. Converting a wxPython app to a C++/wxWindows app
67 should be a straight forward task.
68
69 %----------------------------------------------------------------------
70 \section{Other Python GUIs}\label{wxpother}
71
72 There are other GUI solutions out there for Python.
73
74 \wxheading{Tkinter}
75
76 Tkinter is the defacto standard GUI for Python. It is available
77 on nearly every platform that Python and Tcl/TK are. Why Tcl/Tk?
78 Well because Tkinter is just a wrapper around Tcl's GUI toolkit, Tk.
79 This has its upsides and its downsides...
80
81 The upside is that Tk is a pretty versatile toolkit. It can be made
82 to do a lot of things in a lot of different environments. It is fairly
83 easy to create new widgets and use them interchangeably in your
84 programs.
85
86 The downside is Tcl. When using Tkinter you actually have two
87 separate language interpreters running, the Python interpreter and the
88 Tcl interpreter for the GUI. Since the guts of Tcl is mostly about
89 string processing, it is fairly slow as well. (Not too bad on a fast
90 Pentium II, but you really notice the difference on slower machines.)
91
92 It wasn't until the latest version of Tcl/Tk that native Look and
93 Feel was possible on non-Motif platforms. This is because Tk
94 usually implements its own widgets (controls) even when there are
95 native controls available.
96
97 Tkinter is a pretty low-level toolkit. You have to do a lot of work
98 (verbose program code) to do things that would be much simpler with a higher
99 level of abstraction.
100
101 \wxheading{PythonWin}
102
103 PythonWin is an add-on package for Python for the Win32 platform. It
104 includes wrappers for MFC as well as much of the Win32 API. Because
105 of its foundation, it is very familiar for programmers who have
106 experience with MFC and the Win32 API. It is obviously not compatible
107 with other platforms and toolkits. PythonWin is organized as separate
108 packages and modules so you can use the pieces you need without having
109 to use the GUI portions.
110
111 \wxheading{Others}
112
113 There are quite a few other GUI modules available for Python, some in
114 active use, some that haven't been updated for ages. Most are simple
115 wrappers around some C or C++ toolkit or another, and most are not
116 cross-platform compatible. See \urlref{this link}{http://www.python.org/download/Contributed.html\#Graphics}
117 for a listing of a few of them.
118
119
120 %----------------------------------------------------------------------
121 \section{Using wxPython}\label{wxpusing}
122
123 \wxheading{First things first...}
124
125 I'm not going to try and teach the Python language here. You can do
126 that at the \urlref{Python Tutorial}{http://www.python.org/doc/tut/tut.html}.
127 I'm also going to assume that you know a bit about wxWindows already,
128 enough to notice the similarities in the classes used.
129
130 Take a look at the following wxPython program. You can find a similar
131 program in the {\tt wxPython/demo} directory, named {\tt DialogUnits.py}. If your
132 Python and wxPython are properly installed, you should be able to run
133 it by issuing this command:
134
135 \begin{indented}{1cm}
136 {\bf\tt python DialogUnits.py}
137 \end{indented}
138
139 \hrule
140
141 \begin{verbatim}
142 001: ## import all of the wxPython GUI package
143 002: from wxPython.wx import *
144 003:
145 004: ## Create a new frame class, derived from the wxPython Frame.
146 005: class MyFrame(wxFrame):
147 006:
148 007: def __init__(self, parent, id, title):
149 008: # First, call the base class' __init__ method to create the frame
150 009: wxFrame.__init__(self, parent, id, title,
151 010: wxPoint(100, 100), wxSize(160, 100))
152 011:
153 012: # Associate some events with methods of this class
154 013: EVT_SIZE(self, self.OnSize)
155 014: EVT_MOVE(self, self.OnMove)
156 015:
157 016: # Add a panel and some controls to display the size and position
158 017: panel = wxPanel(self, -1)
159 018: wxStaticText(panel, -1, "Size:",
160 019: wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
161 020: wxStaticText(panel, -1, "Pos:",
162 021: wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
163 022: self.sizeCtrl = wxTextCtrl(panel, -1, "",
164 023: wxDLG_PNT(panel, wxPoint(24, 4)),
165 024: wxDLG_SZE(panel, wxSize(36, -1)),
166 025: wxTE_READONLY)
167 026: self.posCtrl = wxTextCtrl(panel, -1, "",
168 027: wxDLG_PNT(panel, wxPoint(24, 14)),
169 028: wxDLG_SZE(panel, wxSize(36, -1)),
170 029: wxTE_READONLY)
171 030:
172 031:
173 032: # This method is called automatically when the CLOSE event is
174 033: # sent to this window
175 034: def OnCloseWindow(self, event):
176 035: # tell the window to kill itself
177 036: self.Destroy()
178 037:
179 038: # This method is called by the system when the window is resized,
180 039: # because of the association above.
181 040: def OnSize(self, event):
182 041: size = event.GetSize()
183 042: self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
184 043:
185 044: # tell the event system to continue looking for an event handler,
186 045: # so the default handler will get called.
187 046: event.Skip()
188 047:
189 048: # This method is called by the system when the window is moved,
190 049: # because of the association above.
191 050: def OnMove(self, event):
192 051: pos = event.GetPosition()
193 052: self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
194 053:
195 054:
196 055: # Every wxWindows application must have a class derived from wxApp
197 056: class MyApp(wxApp):
198 057:
199 058: # wxWindows calls this method to initialize the application
200 059: def OnInit(self):
201 060:
202 061: # Create an instance of our customized Frame class
203 062: frame = MyFrame(NULL, -1, "This is a test")
204 063: frame.Show(true)
205 064:
206 065: # Tell wxWindows that this is our main window
207 066: self.SetTopWindow(frame)
208 067:
209 068: # Return a success flag
210 069: return true
211 070:
212 071:
213 072: app = MyApp(0) # Create an instance of the application class
214 073: app.MainLoop() # Tell it to start processing events
215 074:
216 \end{verbatim}
217 \hrule
218
219 \wxheading{Things to notice}
220
221 \begin{enumerate}\itemsep=11pt
222 \item At line 2 the wxPython classes, constants, and etc. are imported
223 into the current module's namespace. If you prefer to reduce
224 namespace pollution you can use "{\tt from wxPython import wx}" and
225 then access all the wxPython identifiers through the wx module, for
226 example, "{\tt wx.wxFrame}".
227 \item At line 13 the frame's sizing and moving events are connected to
228 methods of the class. These helper functions are intended to be like
229 the event table macros that wxWindows employs. But since static event
230 tables are impossible with wxPython, we use helpers that are named the
231 same to dynamically build the table. The only real difference is
232 that the first argument to the event helpers is always the window that
233 the event table entry should be added to.
234 \item Notice the use of {\tt wxDLG\_PNT} and {\tt wxDLG\_SZE} in lines 19
235 - 29 to convert from dialog units to pixels. These helpers are unique
236 to wxPython since Python can't do method overloading like C++.
237 \item There is an {\tt OnCloseWindow} method at line 34 but no call to
238 EVT\_CLOSE to attach the event to the method. Does it really get
239 called? The answer is, yes it does. This is because many of the
240 {\em standard} events are attached to windows that have the associated
241 {\em standard} method names. I have tried to follow the lead of the
242 C++ classes in this area to determine what is {\em standard} but since
243 that changes from time to time I can make no guarantees, nor will it
244 be fully documented. When in doubt, use an EVT\_*** function.
245 \item At lines 17 to 21 notice that there are no saved references to
246 the panel or the static text items that are created. Those of you
247 who know Python might be wondering what happens when Python deletes
248 these objects when they go out of scope. Do they disappear from the GUI? They
249 don't. Remember that in wxPython the Python objects are just shadows of the
250 corresponding C++ objects. Once the C++ windows and controls are
251 attached to their parents, the parents manage them and delete them
252 when necessary. For this reason, most wxPython objects do not need to
253 have a \_\_del\_\_ method that explicitly causes the C++ object to be
254 deleted. If you ever have the need to forcibly delete a window, use
255 the Destroy() method as shown on line 36.
256 \item Just like wxWindows in C++, wxPython apps need to create a class
257 derived from {\tt wxApp} (line 56) that implements a method named
258 {\tt OnInit}, (line 59.) This method should create the application's
259 main window (line 62) and use {\tt wxApp.SetTopWindow()} (line 66) to
260 inform wxWindows about it.
261 \item And finally, at line 72 an instance of the application class is
262 created. At this point wxPython finishes initializing itself, and calls
263 the {\tt OnInit} method to get things started. (The zero parameter here is
264 a flag for functionality that isn't quite implemented yet. Just
265 ignore it for now.) The call to {\tt MainLoop} at line 73 starts the event
266 loop which continues until the application terminates or all the top
267 level windows are closed.
268 \end{enumerate}
269
270 %----------------------------------------------------------------------
271 \section{wxWindows classes implemented in wxPython}\label{wxpclasses}
272
273 The following classes are supported in wxPython. Most provide nearly
274 full implementations of the public interfaces specified in the C++
275 documentation, others are less so. They will all be brought as close
276 as possible to the C++ spec over time.
277
278 \begin{itemize}\itemsep=0pt
279 \item \helpref{wxAcceleratorEntry}{wxacceleratorentry}
280 \item \helpref{wxAcceleratorTable}{wxacceleratortable}
281 \item \helpref{wxActivateEvent}{wxactivateevent}
282 \item \helpref{wxBitmap}{wxbitmap}
283 \item \helpref{wxBitmapButton}{wxbitmapbutton}
284 \item \helpref{wxBitmapDataObject}{wxbitmapdataobject}
285 \item wxBMPHandler
286 \item \helpref{wxBoxSizer}{wxboxsizer}
287 \item \helpref{wxBrush}{wxbrush}
288 \item \helpref{wxBusyInfo}{wxbusyinfo}
289 \item \helpref{wxBusyCursor}{wxbusycursor}
290 \item \helpref{wxButton}{wxbutton}
291 \item \helpref{wxCalculateLayoutEvent}{wxcalculatelayoutevent}
292 \item \helpref{wxCalendarCtrl}{wxcalendarctrl}
293 \item wxCaret
294 \item \helpref{wxCheckBox}{wxcheckbox}
295 \item \helpref{wxCheckListBox}{wxchecklistbox}
296 \item \helpref{wxChoice}{wxchoice}
297 \item \helpref{wxClientDC}{wxclientdc}
298 \item \helpref{wxClipboard}{wxclipboard}
299 \item \helpref{wxCloseEvent}{wxcloseevent}
300 \item \helpref{wxColourData}{wxcolourdata}
301 \item \helpref{wxColourDialog}{wxcolourdialog}
302 \item \helpref{wxColour}{wxcolour}
303 \item \helpref{wxComboBox}{wxcombobox}
304 \item \helpref{wxCommandEvent}{wxcommandevent}
305 \item \helpref{wxConfig}{wxconfigbase}
306 \item \helpref{wxControl}{wxcontrol}
307 \item \helpref{wxCursor}{wxcursor}
308 \item \helpref{wxCustomDataObject}{wxcustomdataobject}
309 \item \helpref{wxDataFormat}{wxdataformat}
310 \item \helpref{wxDataObject}{wxdataobject}
311 \item \helpref{wxDataObjectComposite}{wxdataobjectcomposite}
312 \item \helpref{wxDataObjectSimple}{wxdataobjectsimple}
313 \item \helpref{wxDateTime}{wxdatetime}
314 \item \helpref{wxDateSpan}{wxdatespan}
315 \item \helpref{wxDC}{wxdc}
316 \item \helpref{wxDialog}{wxdialog}
317 \item \helpref{wxDirDialog}{wxdirdialog}
318 \item \helpref{wxDragImage}{wxdragimage}
319 \item \helpref{wxDropFilesEvent}{wxdropfilesevent}
320 \item \helpref{wxDropSource}{wxdropsource}
321 \item \helpref{wxDropTarget}{wxdroptarget}
322 \item \helpref{wxEraseEvent}{wxeraseevent}
323 \item \helpref{wxEvent}{wxevent}
324 \item \helpref{wxEvtHandler}{wxevthandler}
325 \item wxFileConfig
326 \item \helpref{wxFileDataObject}{wxfiledataobject}
327 \item \helpref{wxFileDialog}{wxfiledialog}
328 \item \helpref{wxFileDropTarget}{wxfiledroptarget}
329 \item \helpref{wxFileSystem}{wxfilesystem}
330 \item \helpref{wxFileSystemHandler}{wxfilesystemhandler}
331 \item \helpref{wxFocusEvent}{wxfocusevent}
332 \item \helpref{wxFontData}{wxfontdata}
333 \item \helpref{wxFontDialog}{wxfontdialog}
334 \item \helpref{wxFont}{wxfont}
335 \item \helpref{wxFrame}{wxframe}
336 \item \helpref{wxFSFile}{wxfsfile}
337 \item \helpref{wxGauge}{wxgauge}
338 \item wxGIFHandler
339 \item wxGLCanvas
340 \begin{comment}
341 \item wxGridCell
342 \item wxGridEvent
343 \item \helpref{wxGrid}{wxgrid}
344 \end{comment}
345 \item \helpref{wxHtmlCell}{wxhtmlcell}
346 \item \helpref{wxHtmlContainerCell}{wxhtmlcontainercell}
347 \item \helpref{wxHtmlDCRenderer}{wxhtmldcrenderer}
348 \item \helpref{wxHtmlEasyPrinting}{wxhtmleasyprinting}
349 \item \helpref{wxHtmlParser}{wxhtmlparser}
350 \item \helpref{wxHtmlTagHandler}{wxhtmltaghandler}
351 \item \helpref{wxHtmlTag}{wxhtmltag}
352 \item \helpref{wxHtmlWinParser}{wxhtmlwinparser}
353 \item \helpref{wxHtmlPrintout}{wxhtmlprintout}
354 \item \helpref{wxHtmlWinTagHandler}{wxhtmlwintaghandler}
355 \item \helpref{wxHtmlWindow}{wxhtmlwindow}
356 \item wxIconizeEvent
357 \item \helpref{wxIcon}{wxicon}
358 \item \helpref{wxIdleEvent}{wxidleevent}
359 \item \helpref{wxImage}{wximage}
360 \item \helpref{wxImageHandler}{wximagehandler}
361 \item \helpref{wxImageList}{wximagelist}
362 \item \helpref{wxIndividualLayoutConstraint}{wxindividuallayoutconstraint}
363 \item \helpref{wxInitDialogEvent}{wxinitdialogevent}
364 \item \helpref{wxInputStream}{wxinputstream}
365 \item \helpref{wxInternetFSHandler}{fs}
366 \item \helpref{wxJoystickEvent}{wxjoystickevent}
367 \item wxJPEGHandler
368 \item \helpref{wxKeyEvent}{wxkeyevent}
369 \item \helpref{wxLayoutAlgorithm}{wxlayoutalgorithm}
370 \item \helpref{wxLayoutConstraints}{wxlayoutconstraints}
371 \item \helpref{wxListBox}{wxlistbox}
372 \item \helpref{wxListCtrl}{wxlistctrl}
373 \item \helpref{wxListEvent}{wxlistevent}
374 \item \helpref{wxListItem}{wxlistctrlsetitem}
375 \item \helpref{wxMask}{wxmask}
376 \item wxMaximizeEvent
377 \item \helpref{wxMDIChildFrame}{wxmdichildframe}
378 \item \helpref{wxMDIClientWindow}{wxmdiclientwindow}
379 \item \helpref{wxMDIParentFrame}{wxmdiparentframe}
380 \item \helpref{wxMemoryDC}{wxmemorydc}
381 \item \helpref{wxMemoryFSHandler}{wxmemoryfshandler}
382 \item \helpref{wxMenuBar}{wxmenubar}
383 \item \helpref{wxMenuEvent}{wxmenuevent}
384 \item \helpref{wxMenuItem}{wxmenuitem}
385 \item \helpref{wxMenu}{wxmenu}
386 \item \helpref{wxMessageDialog}{wxmessagedialog}
387 \item \helpref{wxMetaFileDC}{wxmetafiledc}
388 \item \helpref{wxMiniFrame}{wxminiframe}
389 \item \helpref{wxMouseEvent}{wxmouseevent}
390 \item \helpref{wxMoveEvent}{wxmoveevent}
391 \item \helpref{wxNotebookEvent}{wxnotebookevent}
392 \item \helpref{wxNotebook}{wxnotebook}
393 \item \helpref{wxPageSetupDialogData}{wxpagesetupdialogdata}
394 \item \helpref{wxPageSetupDialog}{wxpagesetupdialog}
395 \item \helpref{wxPaintDC}{wxpaintdc}
396 \item \helpref{wxPaintEvent}{wxpaintevent}
397 \item \helpref{wxPalette}{wxpalette}
398 \item \helpref{wxPanel}{wxpanel}
399 \item \helpref{wxPen}{wxpen}
400 \item wxPNGHandler
401 \item \helpref{wxPoint}{wxpoint}
402 \item \helpref{wxPostScriptDC}{wxpostscriptdc}
403 \item \helpref{wxPreviewFrame}{wxpreviewframe}
404 \item \helpref{wxPrintData}{wxprintdata}
405 \item \helpref{wxPrintDialogData}{wxprintdialogdata}
406 \item \helpref{wxPrintDialog}{wxprintdialog}
407 \item \helpref{wxPrinter}{wxprinter}
408 \item \helpref{wxPrintPreview}{wxprintpreview}
409 \item \helpref{wxPrinterDC}{wxprinterdc}
410 \item \helpref{wxPrintout}{wxprintout}
411 \item \helpref{wxProcess}{wxprocess}
412 \item \helpref{wxQueryLayoutInfoEvent}{wxquerylayoutinfoevent}
413 \item \helpref{wxRadioBox}{wxradiobox}
414 \item \helpref{wxRadioButton}{wxradiobutton}
415 \item \helpref{wxRealPoint}{wxrealpoint}
416 \item \helpref{wxRect}{wxrect}
417 \item \helpref{wxRegionIterator}{wxregioniterator}
418 \item \helpref{wxRegion}{wxregion}
419 \item \helpref{wxSashEvent}{wxsashevent}
420 \item \helpref{wxSashLayoutWindow}{wxsashlayoutwindow}
421 \item \helpref{wxSashWindow}{wxsashwindow}
422 \item \helpref{wxScreenDC}{wxscreendc}
423 \item \helpref{wxScrollBar}{wxscrollbar}
424 \item \helpref{wxScrollEvent}{wxscrollevent}
425 \item \helpref{wxScrolledWindow}{wxscrolledwindow}
426 \item \helpref{wxScrollWinEvent}{wxscrollwinevent}
427 \item wxShowEvent
428 \item \helpref{wxSingleChoiceDialog}{wxsinglechoicedialog}
429 \item \helpref{wxSizeEvent}{wxsizeevent}
430 \item \helpref{wxSize}{wxsize}
431 \item \helpref{wxSizer}{wxsizer}
432 \item wxSizerItem
433 \item \helpref{wxSlider}{wxslider}
434 \item \helpref{wxSpinButton}{wxspinbutton}
435 \item wxSpinEvent
436 \item \helpref{wxSplitterWindow}{wxsplitterwindow}
437 \item \helpref{wxStaticBitmap}{wxstaticbitmap}
438 \item \helpref{wxStaticBox}{wxstaticbox}
439 \item \helpref{wxStaticBoxSizer}{wxstaticboxsizer}
440 \item \helpref{wxStaticLine}{wxstaticline}
441 \item \helpref{wxStaticText}{wxstatictext}
442 \item \helpref{wxStatusBar}{wxstatusbar}
443 \item \helpref{wxSysColourChangedEvent}{wxsyscolourchangedevent}
444 \item \helpref{wxTaskBarIcon}{wxtaskbaricon}
445 \item \helpref{wxTextCtrl}{wxtextctrl}
446 \item \helpref{wxTextDataObject}{wxtextdataobject}
447 \item \helpref{wxTextDropTarget}{wxtextdroptarget}
448 \item \helpref{wxTextEntryDialog}{wxtextentrydialog}
449 \item \helpref{wxTimer}{wxtimer}
450 \item \helpref{wxTimerEvent}{wxtimerevent}
451 \item \helpref{wxTimeSpan}{wxtimespan}
452 \item \helpref{wxTipProvider}{wxtipprovider}
453 \item wxToolBarTool
454 \item \helpref{wxToolBar}{wxtoolbar}
455 \item wxToolTip
456 \item \helpref{wxTreeCtrl}{wxtreectrl}
457 \item \helpref{wxTreeEvent}{wxtreeevent}
458 \item \helpref{wxTreeItemData}{wxtreeitemdata}
459 \item wxTreeItemId
460 \item \helpref{wxUpdateUIEvent}{wxupdateuievent}
461 \item \helpref{wxValidator}{wxvalidator}
462 \item \helpref{wxWindowDC}{wxwindowdc}
463 \item \helpref{wxWindow}{wxwindow}
464 \item \helpref{wxZipFSHandler}{fs}
465 \end{itemize}
466
467 %----------------------------------------------------------------------
468 \section{Where to go for help}\label{wxphelp}
469
470 Since wxPython is a blending of multiple technologies, help comes from
471 multiple sources. See
472 \urlref{http://wxpython.org/}{http://wxpython.org/} for details on
473 various sources of help, but probably the best source is the
474 wxPython-users mail list. You can view the archive or subscribe by
475 going to
476
477 \urlref{http://lists.wxwindows.org/mailman/listinfo/wxpython-users}{http://lists.wxwindows.org/mailman/listinfo/wxpython-users}
478
479 Or you can send mail directly to the list using this address:
480
481 wxpython-users@lists.wxwindows.org
482
483