]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/wxPython.tex
ugh. Nesting the (per class) plugin sentries can require them to
[wxWidgets.git] / docs / latex / wx / wxPython.tex
... / ...
CommitLineData
1\chapter{wxPython Notes}\label{wxPython}
2
3\setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
4\setfooter{\thepage}{}{}{}{}{\thepage}%
5
6This addendum is written by Robin Dunn, author of the wxPython wrapper
7
8%----------------------------------------------------------------------
9\section{What is wxPython?}\label{wxpwhat}
10
11wxPython is a blending of the wxWindows GUI classes and the
12\urlref{Python}{http://www.python.org/} programming language.
13
14\wxheading{Python}
15
16So what is Python? Go to
17\urlref{http://www.python.org}{http://www.python.org} to learn more,
18but in a nutshell Python is an interpreted,
19interactive, object-oriented programming language. It is often
20compared to Tcl, Perl, Scheme or Java.
21
22Python combines remarkable power with very clear syntax. It has
23modules, classes, exceptions, very high level dynamic data types, and
24dynamic typing. There are interfaces to many system calls and
25libraries, and new built-in modules are easily written in C or
26C++. Python is also usable as an extension language for applications
27that need a programmable interface.
28
29Python is copyrighted but freely usable and distributable, even for
30commercial use.
31
32\wxheading{wxPython}
33
34wxPython is a Python package that can be imported at runtime that
35includes a collection of Python modules and an extension module
36(native code). It provides a series of Python classes that mirror (or
37shadow) many of the wxWindows GUI classes. This extension module
38attempts to mirror the class heirarchy of wxWindows as closely as
39possible. This means that there is a wxFrame class in wxPython that
40looks, smells, tastes and acts almost the same as the wxFrame class in
41the C++ version.
42
43wxPython is very versitile. It can be used to create standalone GUI
44applications, or in situations where Python is embedded in a C++
45application as an internal scripting or macro language.
46
47Currently wxPython is available for Win32 platforms and the GTK
48toolkit (wxGTK) on most Unix/X-windows platforms. See the wxPython
49website \urlref{http://wxPython.org/}{http://wxPython.org/} for
50details about getting wxPython working for you.
51
52%----------------------------------------------------------------------
53\section{Why use wxPython?}\label{wxpwhy}
54
55So why would you want to use wxPython over just C++ and wxWindows?
56Personally I prefer using Python for everything. I only use C++ when I
57absolutely have to eek more performance out of an algorithm, and even
58then I usually code it as an extension module and leave the majority
59of the program in Python.
60
61Another good thing to use wxPython for is quick prototyping of your
62wxWindows apps. With C++ you have to continuously go though the
63edit-compile-link-run cycle, which can be quite time consuming. With
64Python it is only an edit-run cycle. You can easily build an
65application in a few hours with Python that would normally take a few
66days or longer with C++. Converting a wxPython app to a C++/wxWindows app
67should be a straight forward task.
68
69%----------------------------------------------------------------------
70\section{Other Python GUIs}\label{wxpother}
71
72There are other GUI solutions out there for Python.
73
74\wxheading{Tkinter}
75
76Tkinter is the defacto standard GUI for Python. It is available
77on nearly every platform that Python and Tcl/TK are. Why Tcl/Tk?
78Well because Tkinter is just a wrapper around Tcl's GUI toolkit, Tk.
79This has its upsides and its downsides...
80
81The upside is that Tk is a pretty versatile toolkit. It can be made
82to do a lot of things in a lot of different environments. It is fairly
83easy to create new widgets and use them interchangeably in your
84programs.
85
86The downside is Tcl. When using Tkinter you actually have two
87separate language interpreters running, the Python interpreter and the
88Tcl interpreter for the GUI. Since the guts of Tcl is mostly about
89string processing, it is fairly slow as well. (Not too bad on a fast
90Pentium II, but you really notice the difference on slower machines.)
91
92It wasn't until the latest version of Tcl/Tk that native Look and
93Feel was possible on non-Motif platforms. This is because Tk
94usually implements its own widgets (controls) even when there are
95native controls available.
96
97Tkinter 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
99level of abstraction.
100
101\wxheading{PythonWin}
102
103PythonWin is an add-on package for Python for the Win32 platform. It
104includes wrappers for MFC as well as much of the Win32 API. Because
105of its foundation, it is very familiar for programmers who have
106experience with MFC and the Win32 API. It is obviously not compatible
107with other platforms and toolkits. PythonWin is organized as separate
108packages and modules so you can use the pieces you need without having
109to use the GUI portions.
110
111\wxheading{Others}
112
113There are quite a few other GUI modules available for Python, some in
114active use, some that haven't been updated for ages. Most are simple
115wrappers around some C or C++ toolkit or another, and most are not
116cross-platform compatible. See \urlref{this link}{http://www.python.org/download/Contributed.html\#Graphics}
117for a listing of a few of them.
118
119
120%----------------------------------------------------------------------
121\section{Using wxPython}\label{wxpusing}
122
123\wxheading{First things first...}
124
125I'm not going to try and teach the Python language here. You can do
126that at the \urlref{Python Tutorial}{http://www.python.org/doc/tut/tut.html}.
127I'm also going to assume that you know a bit about wxWindows already,
128enough to notice the similarities in the classes used.
129
130Take a look at the following wxPython program. You can find a similar
131program in the {\tt wxPython/demo} directory, named {\tt DialogUnits.py}. If your
132Python and wxPython are properly installed, you should be able to run
133it 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}
142001: ## import all of the wxPython GUI package
143002: from wxPython.wx import *
144003:
145004: ## Create a new frame class, derived from the wxPython Frame.
146005: class MyFrame(wxFrame):
147006:
148007: def __init__(self, parent, id, title):
149008: # First, call the base class' __init__ method to create the frame
150009: wxFrame.__init__(self, parent, id, title,
151010: wxPoint(100, 100), wxSize(160, 100))
152011:
153012: # Associate some events with methods of this class
154013: EVT_SIZE(self, self.OnSize)
155014: EVT_MOVE(self, self.OnMove)
156015:
157016: # Add a panel and some controls to display the size and position
158017: panel = wxPanel(self, -1)
159018: wxStaticText(panel, -1, "Size:",
160019: wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
161020: wxStaticText(panel, -1, "Pos:",
162021: wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
163022: self.sizeCtrl = wxTextCtrl(panel, -1, "",
164023: wxDLG_PNT(panel, wxPoint(24, 4)),
165024: wxDLG_SZE(panel, wxSize(36, -1)),
166025: wxTE_READONLY)
167026: self.posCtrl = wxTextCtrl(panel, -1, "",
168027: wxDLG_PNT(panel, wxPoint(24, 14)),
169028: wxDLG_SZE(panel, wxSize(36, -1)),
170029: wxTE_READONLY)
171030:
172031:
173032: # This method is called automatically when the CLOSE event is
174033: # sent to this window
175034: def OnCloseWindow(self, event):
176035: # tell the window to kill itself
177036: self.Destroy()
178037:
179038: # This method is called by the system when the window is resized,
180039: # because of the association above.
181040: def OnSize(self, event):
182041: size = event.GetSize()
183042: self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
184043:
185044: # tell the event system to continue looking for an event handler,
186045: # so the default handler will get called.
187046: event.Skip()
188047:
189048: # This method is called by the system when the window is moved,
190049: # because of the association above.
191050: def OnMove(self, event):
192051: pos = event.GetPosition()
193052: self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
194053:
195054:
196055: # Every wxWindows application must have a class derived from wxApp
197056: class MyApp(wxApp):
198057:
199058: # wxWindows calls this method to initialize the application
200059: def OnInit(self):
201060:
202061: # Create an instance of our customized Frame class
203062: frame = MyFrame(NULL, -1, "This is a test")
204063: frame.Show(true)
205064:
206065: # Tell wxWindows that this is our main window
207066: self.SetTopWindow(frame)
208067:
209068: # Return a success flag
210069: return true
211070:
212071:
213072: app = MyApp(0) # Create an instance of the application class
214073: app.MainLoop() # Tell it to start processing events
215074:
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
223into the current module's namespace. If you prefer to reduce
224namespace pollution you can use "{\tt from wxPython import wx}" and
225then access all the wxPython identifiers through the wx module, for
226example, "{\tt wx.wxFrame}".
227\item At line 13 the frame's sizing and moving events are connected to
228methods of the class. These helper functions are intended to be like
229the event table macros that wxWindows employs. But since static event
230tables are impossible with wxPython, we use helpers that are named the
231same to dynamically build the table. The only real difference is
232that the first argument to the event helpers is always the window that
233the 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
236to 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
238EVT\_CLOSE to attach the event to the method. Does it really get
239called? 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
242C++ classes in this area to determine what is {\em standard} but since
243that changes from time to time I can make no guarantees, nor will it
244be fully documented. When in doubt, use an EVT\_*** function.
245\item At lines 17 to 21 notice that there are no saved references to
246the panel or the static text items that are created. Those of you
247who know Python might be wondering what happens when Python deletes
248these objects when they go out of scope. Do they disappear from the GUI? They
249don't. Remember that in wxPython the Python objects are just shadows of the
250corresponding C++ objects. Once the C++ windows and controls are
251attached to their parents, the parents manage them and delete them
252when necessary. For this reason, most wxPython objects do not need to
253have a \_\_del\_\_ method that explicitly causes the C++ object to be
254deleted. If you ever have the need to forcibly delete a window, use
255the Destroy() method as shown on line 36.
256\item Just like wxWindows in C++, wxPython apps need to create a class
257derived from {\tt wxApp} (line 56) that implements a method named
258{\tt OnInit}, (line 59.) This method should create the application's
259main window (line 62) and use {\tt wxApp.SetTopWindow()} (line 66) to
260inform wxWindows about it.
261\item And finally, at line 72 an instance of the application class is
262created. At this point wxPython finishes initializing itself, and calls
263the {\tt OnInit} method to get things started. (The zero parameter here is
264a flag for functionality that isn't quite implemented yet. Just
265ignore it for now.) The call to {\tt MainLoop} at line 73 starts the event
266loop which continues until the application terminates or all the top
267level windows are closed.
268\end{enumerate}
269
270%----------------------------------------------------------------------
271\section{wxWindows classes implemented in wxPython}\label{wxpclasses}
272
273The following classes are supported in wxPython. Most provide nearly
274full implementations of the public interfaces specified in the C++
275documentation, others are less so. They will all be brought as close
276as 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
470Since wxPython is a blending of multiple technologies, help comes from
471multiple sources. See
472\urlref{http://wxpython.org/}{http://wxpython.org/} for details on
473various sources of help, but probably the best source is the
474wxPython-users mail list. You can view the archive or subscribe by
475going to
476
477\urlref{http://lists.wxwindows.org/mailman/listinfo/wxpython-users}{http://lists.wxwindows.org/mailman/listinfo/wxpython-users}
478
479Or you can send mail directly to the list using this address:
480
481wxpython-users@lists.wxwindows.org
482
483