]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/wxPython.tex
shows different SetCursor() calls
[wxWidgets.git] / docs / latex / wx / wxPython.tex
CommitLineData
06d20283
RD
1\chapter{wxPython Notes}\label{wxPython}
2\pagenumbering{arabic}%
3\setheader{{\it CHAPTER \thechapter}}{}{}{}{}{{\it CHAPTER \thechapter}}%
4\setfooter{\thepage}{}{}{}{}{\thepage}%
5
6%----------------------------------------------------------------------
7\section{What is wxPython?}\label{wxpwhat}
8
9wxPython is a blending of the wxWindows GUI classes and the
10\urlref{Python}{http://www.python.org/} programming language.
11
12\wxheading{Python}
13
14So what is Python? Go to
15\urlref{http://www.python.org}{http://www.python.org}
16to learn more, but in a nutshell Python is an interpreted,
17interactive, object-oriented programming language. It is often
18compared to Tcl, Perl, Scheme or Java.
19
20Python combines remarkable power with very clear syntax. It has
21modules, classes, exceptions, very high level dynamic data types, and
22dynamic typing. There are interfaces to many system calls and
23libraries, and new built-in modules are easily written in C or
24C++. Python is also usable as an extension language for applications
25that need a programmable interface.
26
27Python is copyrighted but freely usable and distributable, even for
28commercial use.
29
30\wxheading{wxPython}
31
32wxPython is a Python package that can be imported at runtime that
33includes a collection of Python modules and an extension module
34(native code). It provides a series of Python classes that mirror (or
35shadow) many of the wxWindows GUI classes. This extension module
36attempts to mirror the class heiarchy of wxWindows as closely as
37possble. This means that there is a wxFrame class in wxPython that
38looks, smells, tastes and acts almost the same as the wxFrame class in
39the C++ version.
40
41wxPython is very versitile. It can be used to create standalone GUI
42applications, or in situations where Python is embedded in a C++
43application as an internal scripting or macro language.
44
45Currently wxPython is available for Win32 platforms and the GTK
46toolkit (wxGTK) on most *nix/X-windows platforms. The effort to
47enable wxPython for wxMotif will begin shortly. See \helpref{Building
48Python}{wxpbuild} for details about getting wxPython working for you.
49
50
51%----------------------------------------------------------------------
52\section{Why use wxPython?}\label{wxpwhy}
53
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 absolutly have to eek 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 comsuming. 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%----------------------------------------------------------------------
71\section{Other Python GUIs}\label{wxpother}
72
73There are other GUI solutions out there for Python.
74
75\wxheading{Tkinter}
76
77Tkinter is the defacto standard GUI for Python. It is available
78on nearly every platform that Python and Tcl/TK are. Why Tcl/Tk?
79Well because Tkinter is just a wrapper around Tcl's GUI toolkit, Tk.
80This has its upsides and its downsides...
81
82The upside is that Tk is a pretty veristile toolkit. It can be made
83to do a lot of things in a lot of different environments. It is fairly
84easy to create new widgets and use them interchangably in your
85programs.
86
87The downside is Tcl. When using Tkinter you actually have two
88separate language interpreters running, the Python interpreter and the
89Tcl interpreter for the GUI. Since the guts of Tcl is mostly about
90string processing, it is fairly slow as well. (Not too bad on a fast
91Pentium II, but you really notice the difference on slower machines.)
92
93It wasn't until the lastest version of Tcl/Tk that native Look and
94Feel's were possible on non-Motif platforms. This is because Tk
95usually implements it's own widgets (controls) even when there are
96native controls available.
97
98Tkinter is a pretty low-level toolkit. You have to do a lot of work
99(verbose program code) to do things that would be much simpler with a higher
100level of abstraction.
101
102\wxheading{PythonWin}
103
104PythonWin is an add-on package for Python for the Win32 platform. It
105includes wrappers for MFC as well as much of the win32 API. Because
106of its foundation, it is very familiar for programmers who have
107experience with MFC and the Win32 API. It is obviously not compatible
108with other platforms and toolkits. PythonWin is organized as separate
109packages and modules so you can use the pieces you need without having
110to use the GUI portions.
111
112\wxheading{Others}
113
114There are quite a few other GUI modules available for Python, some in
115active use, some that havn't been updated for ages. Most are simple
116wrappers around some C or C++ toolkit or another, and most are not
117cross-platform compatible. See \urlref{this
118link}{http://www.python.org/download/Contributed.html\#Graphics}
119for a listing of a few of them.
120
121
122%----------------------------------------------------------------------
123\section{Building wxPython}\label{wxpbuild}
124
125I used SWIG (\urlref{http://www.swig.org}{http://www.swig.org}) to
126create the source code for the extension module. This enabled me to
127only have to deal with a small amount of code and only have to bother
128with the exceptional issues. SWIG takes care of the rest and
129generates all the repetative code for me. You don't need SWIG to
130build the extension module as all the generated C++ code is included
131under the src directory. If you try to build wxPython and get errors
132because SWIG is missing, then simply touch the .cpp and .py files so
133make won't attempt to build them from the .i files.
134
135I added a few minor features to SWIG to control some of the code
136generation. If you want to play around with this the patches are in
137wxPython/SWIG.patches and they should be applied to the 1.1p5 version
138of SWIG. These new patches are documented at
139\urlref{this site}{http://starship.skyport.net/crew/robind/python/\#swig},
140and they should also end up in the 1.2 version of SWIG.
141
142wxPython is organized as a Python package. This means that the
143directory containing the results of the build process should be a
144subdirectory of a directory on the \tt{PYTHONPATH}, (and preferably
145should be named wxPython.) You can control where the build process
146will dump wxPython by setting the \tt{TARGETDIR} makefile variable.
147The default is \tt{\$(WXWIN)/utils/wxPython}. If you leave it here
148then you should add \tt{\$(WXWIN)/utils} to your \tt{PYTHONPATH}.
149However, you may prefer to use something that is already on your
150\tt{PYTHONPATH}, such as the \tt{site-packages} directory on Unix
151 systems.
152
153
154\wxheading{Win32}
155
156These instructions assume that you have Microsoft Visual C++ 5.0 or
1576.0, that you have installed the command-line tools, and that the
158appropriate environment variables are set for these tools. You should
159also have Python 1.5.1 installed, and wxWindows installed and built as
160specified below.
161
162\begin{enumerate}\itemsep=0pt
163\item Build wxWindows with \tt{wxUSE_RESOURCE_LOADING_IN_MSW} set to 1 in
164\tt{include/wx/msw/setup.h} so icons can be loaded dynamically. While
165there, make sure \tt{wxUSE_OWNER_DRAWN} is also set to 1.
166
167\item Change into the \tt{\$(WXWIN)/utils/wxPython/src} directory.
168
169\item Edit makefile.vc and specify where your python installation is at.
170You may also want to fiddle with the \tt{TARGETDIR} variable as described
171above.
172
173\item Run \tt{nmake -f makefile.vc}
174
175\item If it builds successfully, congratulations! Move on to the next
176step. If not then you can try mailing the wxwin-developers list for
177help. Also, I will always have a pre-built win32 version of this extension module at
178\urlref{http://alldunn.com/wxPython}{http://alldunn.com/wxPython}.
179
180\item Change to the \tt{\$(WXWIN)/utils/wxPython/tests} directory.
181
182\item Try executing the test programs. Note that some of these print
183diagnositc or test info to standard output, so they will require the
184console version of python. For example:
185
186 \tt{python test1.py}
187
188To run them without requiring a console, you can use the \tt{pythonw.exe}
189version of Python either from the command line or from a shortcut.
190
191\end{enumerate}
192
193
194\wxheading{Unix}
195
196These directions assume that you have already successfully built
197wxWindows for GTK, and installed Python 1.5.1. If you build Python
198yourself, you will get everything installed that you need simply by
199doing \bftt{make install}. If you get Python from an RPM or other
200pre-packaged source then there will probably be a separate package
201with the development libraries, etc. that you will need to install.
202
203
204\begin{enumerate}\itemsep=0pt
205\item Change into the \tt{\$(WXWIN)/utils/wxPython/src} directory.
206
207\item Edit \tt{Setup.in} and ensure that the flags, directories, and toolkit
208options are correct, (hopefully this will be done by \tt{configure}
209soon.) See the above commentary about \tt{TARGETDIR}. There are a
210few sample Setup.in.[platform] files provided.
211
212\item Run this command to generate a makefile:
213
214 \tt{make -f Makefile.pre.in boot}
215
216\item Once you have the \tt{Makefile}, run \bftt{make} to build and then
217\bftt{make install} to install the wxPython extension module.
218
219\item Change to the \tt{\$(WXWIN)/utils/wxPython/tests} directory.
220
221\item Try executing the test programs. For example:
222
223 \tt{python test1.py}
224
225\end{enumerate}
226
227
228
229%----------------------------------------------------------------------
230\section{Using wxPython}\label{wxpusing}
231
232\wxheading{First things first...}
233
234I'm not going to try and teach the Python language here. You can do
235that at the \urlref{Python Tutorial}{http://www.python.org/doc/tut/tut.html}.
236I'm also going to assume that you know a bit about wxWindows already,
237enough to notice the similarities in the classes used.
238
239Take a look at the following wxPython program. You can find a similar
240program in the \tt{wxPython/tests} directory, named \tt{test7.py}. If your
241Python and wxPython are properly installed, you should be able to run
242it by issuing this command:
243
244\begin{indented}{1cm}
245 \bftt{python test7.py}
246\end{indented}
247
248\hrule
249
250\begin{verbatim}
251001: ## import all of the wxPython GUI package
252002: from wxPython.wx import *
253003:
254004: ## Create a new frame class, derived from the wxPython Frame.
255005: class MyFrame(wxFrame):
256006:
257007: def __init__(self, parent, id, title):
258008: # First, call the base class' __init__ method to create the frame
259009: wxFrame.__init__(self, parent, id, title,
260010: wxPoint(100, 100), wxSize(160, 100))
261011:
262012: # Associate some events with methods of this class
263013: EVT_SIZE(self, self.OnSize)
264014: EVT_MOVE(self, self.OnMove)
265015:
266016: # Add a panel and some controls to display the size and position
267017: panel = wxPanel(self, -1)
268018: wxStaticText(panel, -1, "Size:",
269019: wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
270020: wxStaticText(panel, -1, "Pos:",
271021: wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
272022: self.sizeCtrl = wxTextCtrl(panel, -1, "",
273023: wxDLG_PNT(panel, wxPoint(24, 4)),
274024: wxDLG_SZE(panel, wxSize(36, -1)),
275025: wxTE_READONLY)
276026: self.posCtrl = wxTextCtrl(panel, -1, "",
277027: wxDLG_PNT(panel, wxPoint(24, 14)),
278028: wxDLG_SZE(panel, wxSize(36, -1)),
279029: wxTE_READONLY)
280030:
281031:
282032: # This method is called automatically when the CLOSE event is
283033: # sent to this window
284034: def OnCloseWindow(self, event):
285035: # tell the window to kill itself
286036: self.Destroy()
287037:
288038: # This method is called by the system when the window is resized,
289039: # because of the association above.
290040: def OnSize(self, event):
291041: size = event.GetSize()
292042: self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
293043:
294044: # tell the event system to continue looking for an event handler,
295045: # so the default handler will get called.
296046: event.Skip()
297047:
298048: # This method is called by the system when the window is moved,
299049: # because of the association above.
300050: def OnMove(self, event):
301051: pos = event.GetPosition()
302052: self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
303053:
304054:
305055: # Every wxWindows application must have a class derived from wxApp
306056: class MyApp(wxApp):
307057:
308058: # wxWindows calls this method to initialize the application
309059: def OnInit(self):
310060:
311061: # Create an instance of our customized Frame class
312062: frame = MyFrame(NULL, -1, "This is a test")
313063: frame.Show(true)
314064:
315065: # Tell wxWindows that this is our main window
316066: self.SetTopWindow(frame)
317067:
318068: # Return a success flag
319069: return true
320070:
321071:
322072: app = MyApp(0) # Create an instance of the application class
323073: app.MainLoop() # Tell it to start processing events
324074:
325\end{verbatim}
326\hrule
327
328\wxheading{Things to notice:}\begin{enumerate}\itemsep=0pt
329\item At line 2 the wxPython classes, constants, and etc. are imported
330into the current module's namespace. If you prefer to reduce
331namespace polution you can use "\tt{from wxPython import wx}" and
332then access all the wxPython identifiers through the wx module, for
333example, "\tt{wx.wxFrame}".
334
335\item At line 13 the frame's sizing and moving events are connected to
336methods of the class. These helper functions are intended to be like
337the event table macros that wxWindows employs. But since static event
338tables are impossible with wxPython, we use helpers that are named the
339same to dynamically build the table. The only real difference is
340that the first arguemnt to the event helpers is always the window that
341the event table entry should be added to.
342
343\item Notice the use of \tt{wxDLG_PNT} and \tt{wxDLG_SZE} in lines 19
344- 29 to convert from dialog units to pixels. These helpers are unique
345to wxPython since Python can't do method overloading like C++.
346
347\item There is an \tt{OnCloseWindow} method at line 34 but no call to
348EVT_CLOSE to attach the event to the method. Does it really get
349called? The answer is, yes it does. This is because many of the
350\em{standard} events are attached to windows that have the associated
351\em{standard} method names. I have tried to follow the lead of the
352C++ classes in this area to determine what is \em{standard} but since
353that changes from time to time I can make no guarentees, nor will it
354be fully documented. When in doubt, use an EVT_*** function.
355
356\item At lines 17 to 21 notice that there are no saved references to
357the panel or the static text items that are created. Those of you
358who know Python might be wondering what happens when Python deletes
359these objects when they go out of scope. Do they disappear from the GUI? They
360don't. Remember that in wxPython the Python objects are just shadows of the
361coresponding C++ objects. Once the C++ windows and controls are
362attached to their parents, the parents manage them and delete them
363when necessary. For this reason, most wxPython objects do not need to
364have a __del__ method that explicitly causes the C++ object to be
365deleted. If you ever have the need to forcibly delete a window, use
366the Destroy() method as shown on line 36.
367
368\item Just like wxWindows in C++, wxPython apps need to create a class
369derived from \tt{wxApp} (line 56) that implements a method named
370\tt{OnInit}, (line 59.) This method should create the application's
371main window (line 62) and use \tt{wxApp.SetTopWindow()} (line 66) to
372inform wxWindows about it.
373
374\item And finally, at line 72 an instance of the application class is
375created. At this point wxPython finishes initializing itself, and calls
376the \tt{OnInit} method to get things started. (The zero parameter here is
377a flag for functionality that isn't quite implemented yet. Just
378ignore it for now.) The call to \tt{MainLoop} at line 73 starts the event
379loop which continues until the application terminates or all the top
380level windows are closed.
381
382\end{enumerate}
383
384
385
386%----------------------------------------------------------------------
387\section{wxWindows classes implemented in wxPython}\label{wxpclasses}
388
389The following classes are supported in wxPython. Most provide nearly
390full implementations of the public interfaces specified in the C++
391documentation, others are less so. They will all be brought as close
392as possible to the C++ spec over time.
393
394\begin{itemize}\itemsep=0pt
395\item \helpref{wxAcceleratorEntry}{wxacceleratorentry}
396\item \helpref{wxAcceleratorTable}{wxacceleratortable}
397\item \helpref{wxActivateEvent}{wxactivateevent}
398\item \helpref{wxBitmapButton}{wxbitmapbutton}
399\item \helpref{wxBitmap}{wxbitmap}
400\item \helpref{wxBrush}{wxbrush}
401\item \helpref{wxButton}{wxbutton}
402\item \helpref{wxCalculateLayoutEvent}{wxcalculatelayoutevent}
403\item \helpref{wxCheckBox}{wxcheckbox}
404\item \helpref{wxCheckListBox}{wxchecklistbox}
405\item \helpref{wxChoice}{wxchoice}
406\item \helpref{wxClientDC}{wxclientdc}
407\item \helpref{wxCloseEvent}{wxcloseevent}
408\item \helpref{wxColourData}{wxcolourdata}
409\item \helpref{wxColourDialog}{wxcolourdialog}
410\item \helpref{wxColour}{wxcolour}
411\item \helpref{wxComboBox}{wxcombobox}
412\item \helpref{wxCommandEvent}{wxcommandevent}
413\item \helpref{wxConfig}{wxconfigbase}
414\item \helpref{wxControl}{wxcontrol}
415\item \helpref{wxCursor}{wxcursor}
416\item \helpref{wxDC}{wxdc}
417\item \helpref{wxDialog}{wxdialog}
418\item \helpref{wxDirDialog}{wxdirdialog}
419\item \helpref{wxDropFilesEvent}{wxdropfilesevent}
420\item \helpref{wxEraseEvent}{wxeraseevent}
421\item \helpref{wxEvent}{wxevent}
422\item \helpref{wxEvtHandler}{wxevthandler}
423\item \helpref{wxFileDialog}{wxfiledialog}
424\item \helpref{wxFocusEvent}{wxfocusevent}
425\item \helpref{wxFontData}{wxfontdata}
426\item \helpref{wxFontDialog}{wxfontdialog}
427\item \helpref{wxFont}{wxfont}
428\item \helpref{wxFrame}{wxframe}
429\item \helpref{wxGauge}{wxgauge}
fd34e3a5
JS
430\item wxGridCell
431\item wxGridEvent
06d20283 432\item \helpref{wxGrid}{wxgrid}
fd34e3a5 433\item wxIconizeEvent
06d20283
RD
434\item \helpref{wxIcon}{wxicon}
435\item \helpref{wxIdleEvent}{wxidleevent}
436\item \helpref{wxImageList}{wximagelist}
437\item \helpref{wxIndividualLayoutConstraint}{wxindividuallayoutconstraint}
438\item \helpref{wxInitDialogEvent}{wxinitdialogevent}
439\item \helpref{wxJoystickEvent}{wxjoystickevent}
440\item \helpref{wxKeyEvent}{wxkeyevent}
441\item \helpref{wxLayoutAlgorithm}{wxlayoutalgorithm}
442\item \helpref{wxLayoutConstraints}{wxlayoutconstraints}
443\item \helpref{wxListBox}{wxlistbox}
444\item \helpref{wxListCtrl}{wxlistctrl}
445\item \helpref{wxListEvent}{wxlistevent}
21f280f4 446\item \helpref{wxListItem}{wxlistctrlsetitem}
06d20283
RD
447\item \helpref{wxMDIChildFrame}{wxmdichildframe}
448\item \helpref{wxMDIClientWindow}{wxmdiclientwindow}
449\item \helpref{wxMDIParentFrame}{wxmdiparentframe}
450\item \helpref{wxMask}{wxmask}
fd34e3a5 451\item wxMaximizeEvent
06d20283
RD
452\item \helpref{wxMemoryDC}{wxmemorydc}
453\item \helpref{wxMenuBar}{wxmenubar}
454\item \helpref{wxMenuEvent}{wxmenuevent}
455\item \helpref{wxMenuItem}{wxmenuitem}
456\item \helpref{wxMenu}{wxmenu}
457\item \helpref{wxMessageDialog}{wxmessagedialog}
458\item \helpref{wxMetaFileDC}{wxmetafiledc}
459\item \helpref{wxMiniFrame}{wxminiframe}
460\item \helpref{wxMouseEvent}{wxmouseevent}
461\item \helpref{wxMoveEvent}{wxmoveevent}
462\item \helpref{wxNotebookEvent}{wxnotebookevent}
463\item \helpref{wxNotebook}{wxnotebook}
464\item \helpref{wxPageSetupData}{wxpagesetupdata}
465\item \helpref{wxPageSetupDialog}{wxpagesetupdialog}
466\item \helpref{wxPaintDC}{wxpaintdc}
467\item \helpref{wxPaintEvent}{wxpaintevent}
468\item \helpref{wxPalette}{wxpalette}
469\item \helpref{wxPanel}{wxpanel}
470\item \helpref{wxPen}{wxpen}
471\item \helpref{wxPoint}{wxpoint}
472\item \helpref{wxPostScriptDC}{wxpostscriptdc}
473\item \helpref{wxPrintData}{wxprintdata}
474\item \helpref{wxPrintDialog}{wxprintdialog}
475\item \helpref{wxPrinterDC}{wxprinterdc}
476\item \helpref{wxQueryLayoutInfoEvent}{wxquerylayoutinfoevent}
477\item \helpref{wxRadioBox}{wxradiobox}
478\item \helpref{wxRadioButton}{wxradiobutton}
479\item \helpref{wxRealPoint}{wxrealpoint}
480\item \helpref{wxRect}{wxrect}
481\item \helpref{wxRegionIterator}{wxregioniterator}
482\item \helpref{wxRegion}{wxregion}
483\item \helpref{wxSashEvent}{wxsashevent}
484\item \helpref{wxSashLayoutWindow}{wxsashlayoutwindow}
485\item \helpref{wxSashWindow}{wxsashwindow}
486\item \helpref{wxScreenDC}{wxscreendc}
487\item \helpref{wxScrollBar}{wxscrollbar}
488\item \helpref{wxScrollEvent}{wxscrollevent}
489\item \helpref{wxScrolledWindow}{wxscrolledwindow}
fd34e3a5 490\item wxShowEvent
06d20283
RD
491\item \helpref{wxSingleChoiceDialog}{wxsinglechoicedialog}
492\item \helpref{wxSizeEvent}{wxsizeevent}
493\item \helpref{wxSize}{wxsize}
494\item \helpref{wxSlider}{wxslider}
495\item \helpref{wxSpinButton}{wxspinbutton}
fd34e3a5 496\item wxSpinEvent
06d20283
RD
497\item \helpref{wxSplitterWindow}{wxsplitterwindow}
498\item \helpref{wxStaticBitmap}{wxstaticbitmap}
499\item \helpref{wxStaticBox}{wxstaticbox}
500\item \helpref{wxStaticText}{wxstatictext}
501\item \helpref{wxStatusBar}{wxstatusbar}
502\item \helpref{wxSysColourChangedEvent}{wxsyscolourchangedevent}
503\item \helpref{wxTaskBarIcon}{wxtaskbaricon}
504\item \helpref{wxTextCtrl}{wxtextctrl}
505\item \helpref{wxTextEntryDialog}{wxtextentrydialog}
506\item \helpref{wxTimer}{wxtimer}
fd34e3a5 507\item wxToolBarTool
06d20283 508\item \helpref{wxToolBar}{wxtoolbar}
fd34e3a5 509\item wxToolTip
06d20283
RD
510\item \helpref{wxTreeCtrl}{wxtreectrl}
511\item \helpref{wxTreeEvent}{wxtreeevent}
512\item \helpref{wxTreeItemData}{wxtreeitemdata}
fd34e3a5 513\item wxTreeItemId
06d20283
RD
514\item \helpref{wxUpdateUIEvent}{wxupdateuievent}
515\item \helpref{wxWindowDC}{wxwindowdc}
516\item \helpref{wxWindow}{wxwindow}
517\end{itemize}
518
519%----------------------------------------------------------------------
520\section{Where to go for help}\label{wxphelp}
521
522Since wxPython is a blending of multiple technologies, help comes from
523multiple sources. See
524\urlref{http://alldunn.com/wxPython}{http://alldunn.com/wxPython} for details on
525various sources of help, but probably the best source is the
526wxPython-users mail list. You can view the archive or subscribe by
527going to
528
529\urlref{http://starship.python.net/mailman/listinfo/wxpython-users}{http://starship.python.net/mailman/listinfo/wxpython-users}
530
531Or you can send mail directly to the list using this address:
532
533wxpython-users@starship.python.net
534
535
536
537
538
539