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