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