]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/wxPython.tex
Add wxImage::InitAllHandlers()
[wxWidgets.git] / docs / latex / wx / wxPython.tex
... / ...
CommitLineData
1\chapter{wxPython Notes}\label{wxPython}
2\pagenumbering{arabic}%
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 heiarchy of wxWindows as closely as
39possble. 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. The effort to
49enable wxPython for wxMotif will begin shortly. See \helpref{Building Python}{wxpbuild} 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
57I absolutely have to eke more performance out of an algorithm, and even
58then I ususally 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 interchangably 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 lastest 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 havn'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\section{Building wxPython}\label{wxpbuild}
121
122I used SWIG (\urlref{http://www.swig.org}{http://www.swig.org}) to
123to create the source code for the
124extension module. This enabled me to only have to deal with a small
125amount of code and only have to bother with the exceptional issues.
126SWIG takes care of the rest and generates all the repetative code for
127me. You don't need SWIG to build the extension module as all the
128generated C++ code is included under the src directory.
129
130I added a few minor features to SWIG to control some of the code
131generation. If you want to play around with this you will need to get
132a recent version of SWIG from their CVS or from a daily build. See
133\urlref{http://www.swig.org/}{http://www.swig.org/} for details.
134
135wxPython is organized as a Python package. This means that the
136directory containing the results of the build process should be a
137subdirectory of a directory on the \tt{PYTHONPATH}. (And preferably should
138be named wxPython.) You can control where the build process will dump
139wxPython by setting the \tt{TARGETDIR} variable for the build utility (see
140below).
141
142\begin{enumerate}\itemsep=0pt
143\item Build wxWindows as described in its BuildCVS.txt file. For Unix
144systems I run configure with these flags:
145
146\begin{verbatim}
147 --with-gtk
148 --with-libjpeg
149 --without-odbc
150 --enable-unicode=no
151 --enable-threads=yes
152 --enable-socket=yes
153 --enable-static=no
154 --enable-shared=yes
155 --disable-std_iostreams
156\end{verbatim}
157
158You can use whatever flags you want, but I know these work.
159
160For Win32 systems I use Visual C++ 6.0, but 5.0 should work also. The
161build utility currently does not support any other Win32 compilers.
162\item At this point you may want to make an alias or symlink, script,
163batch file, whatever on the PATH that invokes \tt{\$(WXWIN)/utils/wxPython/distrib/build.py} to
164help simplify matters somewhat. For example, on my Win32 system I have a file named
165 \tt{build}.bat in a directory on the PATH that contains:
166
167\tt{python \%WXWIN/utils/wxPython/distrib/build.py \%1 \%2 \%3 \%4 \%5 \%6}
168
169\item Change into the \tt{\$(WXWIN)/utils/wxPython/src} directory.
170\item Type "\tt{build -b}" to build wxPython and "\tt{build -i}" to
171install it, or "\tt{build -bi}" to do both steps at once.
172
173The build.py script actually generates a Makefile based on what it
174finds on your system and information found in the build.cfg file.
175If you have troubles building or you want it built or installed in
176a different way, take a look at the docstring in build.py. You are
177able to to override many configuration options in a file named
178build.local.
179\item To build and install the add-on modules, change to the appropriate
180directory under \tt{\$(WXWIN)/utils/wxPython/modules} and run the build
181utility again.
182\item Change to the \tt{\$(WXWIN)/utils/wxPython/demo} directory.
183\item Try executing the demo program. For example:
184
185\tt{python demo.py}
186
187To run it without requiring a console on Win32, you can use the
188\tt{pythonw.exe} version of Python either from the command line or from a
189shortcut.
190\end{enumerate}
191
192%----------------------------------------------------------------------
193\section{Using wxPython}\label{wxpusing}
194
195\wxheading{First things first...}
196
197I'm not going to try and teach the Python language here. You can do
198that at the \urlref{Python Tutorial}{http://www.python.org/doc/tut/tut.html}.
199I'm also going to assume that you know a bit about wxWindows already,
200enough to notice the similarities in the classes used.
201
202Take a look at the following wxPython program. You can find a similar
203program in the \tt{wxPython/demo} directory, named \tt{DialogUnits.py}. If your
204Python and wxPython are properly installed, you should be able to run
205it by issuing this command:
206
207\begin{indented}{1cm}
208 \bftt{python DialogUnits.py}
209\end{indented}
210
211\hrule
212
213\begin{verbatim}
214001: ## import all of the wxPython GUI package
215002: from wxPython.wx import *
216003:
217004: ## Create a new frame class, derived from the wxPython Frame.
218005: class MyFrame(wxFrame):
219006:
220007: def __init__(self, parent, id, title):
221008: # First, call the base class' __init__ method to create the frame
222009: wxFrame.__init__(self, parent, id, title,
223010: wxPoint(100, 100), wxSize(160, 100))
224011:
225012: # Associate some events with methods of this class
226013: EVT_SIZE(self, self.OnSize)
227014: EVT_MOVE(self, self.OnMove)
228015:
229016: # Add a panel and some controls to display the size and position
230017: panel = wxPanel(self, -1)
231018: wxStaticText(panel, -1, "Size:",
232019: wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
233020: wxStaticText(panel, -1, "Pos:",
234021: wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
235022: self.sizeCtrl = wxTextCtrl(panel, -1, "",
236023: wxDLG_PNT(panel, wxPoint(24, 4)),
237024: wxDLG_SZE(panel, wxSize(36, -1)),
238025: wxTE_READONLY)
239026: self.posCtrl = wxTextCtrl(panel, -1, "",
240027: wxDLG_PNT(panel, wxPoint(24, 14)),
241028: wxDLG_SZE(panel, wxSize(36, -1)),
242029: wxTE_READONLY)
243030:
244031:
245032: # This method is called automatically when the CLOSE event is
246033: # sent to this window
247034: def OnCloseWindow(self, event):
248035: # tell the window to kill itself
249036: self.Destroy()
250037:
251038: # This method is called by the system when the window is resized,
252039: # because of the association above.
253040: def OnSize(self, event):
254041: size = event.GetSize()
255042: self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
256043:
257044: # tell the event system to continue looking for an event handler,
258045: # so the default handler will get called.
259046: event.Skip()
260047:
261048: # This method is called by the system when the window is moved,
262049: # because of the association above.
263050: def OnMove(self, event):
264051: pos = event.GetPosition()
265052: self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
266053:
267054:
268055: # Every wxWindows application must have a class derived from wxApp
269056: class MyApp(wxApp):
270057:
271058: # wxWindows calls this method to initialize the application
272059: def OnInit(self):
273060:
274061: # Create an instance of our customized Frame class
275062: frame = MyFrame(NULL, -1, "This is a test")
276063: frame.Show(true)
277064:
278065: # Tell wxWindows that this is our main window
279066: self.SetTopWindow(frame)
280067:
281068: # Return a success flag
282069: return true
283070:
284071:
285072: app = MyApp(0) # Create an instance of the application class
286073: app.MainLoop() # Tell it to start processing events
287074:
288\end{verbatim}
289\hrule
290
291\wxheading{Things to notice}
292
293\begin{enumerate}\itemsep=0pt
294\item At line 2 the wxPython classes, constants, and etc. are imported
295into the current module's namespace. If you prefer to reduce
296namespace pollution you can use "\tt{from wxPython import wx}" and
297then access all the wxPython identifiers through the wx module, for
298example, "\tt{wx.wxFrame}".
299\item At line 13 the frame's sizing and moving events are connected to
300methods of the class. These helper functions are intended to be like
301the event table macros that wxWindows employs. But since static event
302tables are impossible with wxPython, we use helpers that are named the
303same to dynamically build the table. The only real difference is
304that the first arguemnt to the event helpers is always the window that
305the event table entry should be added to.
306\item Notice the use of \tt{wxDLG\_PNT} and \tt{wxDLG\_SZE} in lines 19
307- 29 to convert from dialog units to pixels. These helpers are unique
308to wxPython since Python can't do method overloading like C++.
309\item There is an \tt{OnCloseWindow} method at line 34 but no call to
310EVT\_CLOSE to attach the event to the method. Does it really get
311called? The answer is, yes it does. This is because many of the
312\em{standard} events are attached to windows that have the associated
313\em{standard} method names. I have tried to follow the lead of the
314C++ classes in this area to determine what is \em{standard} but since
315that changes from time to time I can make no guarentees, nor will it
316be fully documented. When in doubt, use an EVT\_*** function.
317\item At lines 17 to 21 notice that there are no saved references to
318the panel or the static text items that are created. Those of you
319who know Python might be wondering what happens when Python deletes
320these objects when they go out of scope. Do they disappear from the GUI? They
321don't. Remember that in wxPython the Python objects are just shadows of the
322coresponding C++ objects. Once the C++ windows and controls are
323attached to their parents, the parents manage them and delete them
324when necessary. For this reason, most wxPython objects do not need to
325have a \_\_del\_\_ method that explicitly causes the C++ object to be
326deleted. If you ever have the need to forcibly delete a window, use
327the Destroy() method as shown on line 36.
328\item Just like wxWindows in C++, wxPython apps need to create a class
329derived from \tt{wxApp} (line 56) that implements a method named
330\tt{OnInit}, (line 59.) This method should create the application's
331main window (line 62) and use \tt{wxApp.SetTopWindow()} (line 66) to
332inform wxWindows about it.
333\item And finally, at line 72 an instance of the application class is
334created. At this point wxPython finishes initializing itself, and calls
335the \tt{OnInit} method to get things started. (The zero parameter here is
336a flag for functionality that isn't quite implemented yet. Just
337ignore it for now.) The call to \tt{MainLoop} at line 73 starts the event
338loop which continues until the application terminates or all the top
339level windows are closed.
340\end{enumerate}
341
342%----------------------------------------------------------------------
343\section{wxWindows classes implemented in wxPython}\label{wxpclasses}
344
345The following classes are supported in wxPython. Most provide nearly
346full implementations of the public interfaces specified in the C++
347documentation, others are less so. They will all be brought as close
348as possible to the C++ spec over time.
349
350\begin{itemize}\itemsep=0pt
351\item \helpref{wxAcceleratorEntry}{wxacceleratorentry}
352\item \helpref{wxAcceleratorTable}{wxacceleratortable}
353\item \helpref{wxActivateEvent}{wxactivateevent}
354\item \helpref{wxBitmapButton}{wxbitmapbutton}
355\item \helpref{wxBitmap}{wxbitmap}
356\item wxBMPHandler
357\item \helpref{wxBrush}{wxbrush}
358\item \helpref{wxButton}{wxbutton}
359\item \helpref{wxCalculateLayoutEvent}{wxcalculatelayoutevent}
360\item \helpref{wxCheckBox}{wxcheckbox}
361\item \helpref{wxCheckListBox}{wxchecklistbox}
362\item \helpref{wxChoice}{wxchoice}
363\item \helpref{wxClientDC}{wxclientdc}
364\item \helpref{wxCloseEvent}{wxcloseevent}
365\item \helpref{wxColourData}{wxcolourdata}
366\item \helpref{wxColourDialog}{wxcolourdialog}
367\item \helpref{wxColour}{wxcolour}
368\item \helpref{wxComboBox}{wxcombobox}
369\item \helpref{wxCommandEvent}{wxcommandevent}
370\item \helpref{wxConfig}{wxconfigbase}
371\item \helpref{wxControl}{wxcontrol}
372\item \helpref{wxCursor}{wxcursor}
373\item \helpref{wxDC}{wxdc}
374\item \helpref{wxDialog}{wxdialog}
375\item \helpref{wxDirDialog}{wxdirdialog}
376\item \helpref{wxDropFilesEvent}{wxdropfilesevent}
377\item \helpref{wxEraseEvent}{wxeraseevent}
378\item \helpref{wxEvent}{wxevent}
379\item \helpref{wxEvtHandler}{wxevthandler}
380\item \helpref{wxFileDialog}{wxfiledialog}
381\item \helpref{wxFocusEvent}{wxfocusevent}
382\item \helpref{wxFontData}{wxfontdata}
383\item \helpref{wxFontDialog}{wxfontdialog}
384\item \helpref{wxFont}{wxfont}
385\item \helpref{wxFrame}{wxframe}
386\item \helpref{wxGauge}{wxgauge}
387\item wxGIFHandler
388\item wxGLCanvas
389\item wxGridCell
390\item wxGridEvent
391\item \helpref{wxGrid}{wxgrid}
392\item wxIconizeEvent
393\item \helpref{wxIcon}{wxicon}
394\item \helpref{wxIdleEvent}{wxidleevent}
395\item \helpref{wxImage}{wximage}
396\item \helpref{wxImageHandler}{wximagehandler}
397\item \helpref{wxImageList}{wximagelist}
398\item \helpref{wxIndividualLayoutConstraint}{wxindividuallayoutconstraint}
399\item \helpref{wxInitDialogEvent}{wxinitdialogevent}
400\item \helpref{wxJoystickEvent}{wxjoystickevent}
401\item wxJPEGHandler
402\item \helpref{wxKeyEvent}{wxkeyevent}
403\item \helpref{wxLayoutAlgorithm}{wxlayoutalgorithm}
404\item \helpref{wxLayoutConstraints}{wxlayoutconstraints}
405\item \helpref{wxListBox}{wxlistbox}
406\item \helpref{wxListCtrl}{wxlistctrl}
407\item \helpref{wxListEvent}{wxlistevent}
408\item \helpref{wxListItem}{wxlistctrlsetitem}
409\item \helpref{wxMDIChildFrame}{wxmdichildframe}
410\item \helpref{wxMDIClientWindow}{wxmdiclientwindow}
411\item \helpref{wxMDIParentFrame}{wxmdiparentframe}
412\item \helpref{wxMask}{wxmask}
413\item wxMaximizeEvent
414\item \helpref{wxMemoryDC}{wxmemorydc}
415\item \helpref{wxMenuBar}{wxmenubar}
416\item \helpref{wxMenuEvent}{wxmenuevent}
417\item \helpref{wxMenuItem}{wxmenuitem}
418\item \helpref{wxMenu}{wxmenu}
419\item \helpref{wxMessageDialog}{wxmessagedialog}
420\item \helpref{wxMetaFileDC}{wxmetafiledc}
421\item \helpref{wxMiniFrame}{wxminiframe}
422\item \helpref{wxMouseEvent}{wxmouseevent}
423\item \helpref{wxMoveEvent}{wxmoveevent}
424\item \helpref{wxNotebookEvent}{wxnotebookevent}
425\item \helpref{wxNotebook}{wxnotebook}
426\item \helpref{wxPageSetupDialogData}{wxpagesetupdialogdata}
427\item \helpref{wxPageSetupDialog}{wxpagesetupdialog}
428\item \helpref{wxPaintDC}{wxpaintdc}
429\item \helpref{wxPaintEvent}{wxpaintevent}
430\item \helpref{wxPalette}{wxpalette}
431\item \helpref{wxPanel}{wxpanel}
432\item \helpref{wxPen}{wxpen}
433\item wxPNGHandler
434\item \helpref{wxPoint}{wxpoint}
435\item \helpref{wxPostScriptDC}{wxpostscriptdc}
436\item \helpref{wxPreviewFrame}{wxpreviewframe}
437\item \helpref{wxPrintData}{wxprintdata}
438\item \helpref{wxPrintDialogData}{wxprintdialogdata}
439\item \helpref{wxPrintDialog}{wxprintdialog}
440\item \helpref{wxPrinter}{wxprinter}
441\item \helpref{wxPrintPreview}{wxprintpreview}
442\item \helpref{wxPrinterDC}{wxprinterdc}
443\item \helpref{wxPrintout}{wxprintout}
444\item \helpref{wxQueryLayoutInfoEvent}{wxquerylayoutinfoevent}
445\item \helpref{wxRadioBox}{wxradiobox}
446\item \helpref{wxRadioButton}{wxradiobutton}
447\item \helpref{wxRealPoint}{wxrealpoint}
448\item \helpref{wxRect}{wxrect}
449\item \helpref{wxRegionIterator}{wxregioniterator}
450\item \helpref{wxRegion}{wxregion}
451\item \helpref{wxSashEvent}{wxsashevent}
452\item \helpref{wxSashLayoutWindow}{wxsashlayoutwindow}
453\item \helpref{wxSashWindow}{wxsashwindow}
454\item \helpref{wxScreenDC}{wxscreendc}
455\item \helpref{wxScrollBar}{wxscrollbar}
456\item \helpref{wxScrollEvent}{wxscrollevent}
457\item \helpref{wxScrolledWindow}{wxscrolledwindow}
458\item wxShowEvent
459\item \helpref{wxSingleChoiceDialog}{wxsinglechoicedialog}
460\item \helpref{wxSizeEvent}{wxsizeevent}
461\item \helpref{wxSize}{wxsize}
462\item \helpref{wxSlider}{wxslider}
463\item \helpref{wxSpinButton}{wxspinbutton}
464\item wxSpinEvent
465\item \helpref{wxSplitterWindow}{wxsplitterwindow}
466\item \helpref{wxStaticBitmap}{wxstaticbitmap}
467\item \helpref{wxStaticBox}{wxstaticbox}
468\item wxStaticLine
469\item \helpref{wxStaticText}{wxstatictext}
470\item \helpref{wxStatusBar}{wxstatusbar}
471\item \helpref{wxSysColourChangedEvent}{wxsyscolourchangedevent}
472\item \helpref{wxTaskBarIcon}{wxtaskbaricon}
473\item \helpref{wxTextCtrl}{wxtextctrl}
474\item \helpref{wxTextEntryDialog}{wxtextentrydialog}
475\item \helpref{wxTimer}{wxtimer}
476\item wxToolBarTool
477\item \helpref{wxToolBar}{wxtoolbar}
478\item wxToolTip
479\item \helpref{wxTreeCtrl}{wxtreectrl}
480\item \helpref{wxTreeEvent}{wxtreeevent}
481\item \helpref{wxTreeItemData}{wxtreeitemdata}
482\item wxTreeItemId
483\item \helpref{wxUpdateUIEvent}{wxupdateuievent}
484\item \helpref{wxWindowDC}{wxwindowdc}
485\item \helpref{wxWindow}{wxwindow}
486\end{itemize}
487
488%----------------------------------------------------------------------
489\section{Where to go for help}\label{wxphelp}
490
491Since wxPython is a blending of multiple technologies, help comes from
492multiple sources. See
493\urlref{http://alldunn.com/wxPython}{http://alldunn.com/wxPython} for details on
494various sources of help, but probably the best source is the
495wxPython-users mail list. You can view the archive or subscribe by
496going to
497
498\urlref{http://starship.python.net/mailman/listinfo/wxpython-users}{http://starship.python.net/mailman/listinfo/wxpython-users}
499
500Or you can send mail directly to the list using this address:
501
502wxpython-users@starship.python.net
503