]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/python.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
11 @page overview_python wxPython Overview
13 This topic was written by Robin Dunn, author of the wxPython wrapper.
15 @li @ref overview_python_what
16 @li @ref overview_python_why
17 @li @ref overview_python_othergui
18 @li @ref overview_python_using
19 @li @ref overview_python_classes
20 @li @ref overview_python_help
26 @section overview_python_what What is wxPython?
28 wxPython is a blending of the wxWidgets GUI classes and the Python programming
31 @subsection overview_python_what_py Python
33 So what is Python? Go to http://www.python.org to learn more, but in a
34 nutshell Python is an interpreted, interactive, object-oriented programming
35 language. It is often compared to Tcl, Perl, Scheme or Java.
37 Python combines remarkable power with very clear syntax. It has modules,
38 classes, exceptions, very high level dynamic data types, and dynamic typing.
39 There are interfaces to many system calls and libraries, and new built-in
40 modules are easily written in C or C++. Python is also usable as an extension
41 language for applications that need a programmable interface.
43 Python is copyrighted but freely usable and distributable, even for commercial
46 @subsection overview_python_what_wxpy wxPython
48 wxPython is a Python package that can be imported at runtime that includes a
49 collection of Python modules and an extension module (native code). It provides
50 a series of Python classes that mirror (or shadow) many of the wxWidgets GUI
51 classes. This extension module attempts to mirror the class hierarchy of
52 wxWidgets as closely as possible. This means that there is a wxFrame class in
53 wxPython that looks, smells, tastes and acts almost the same as the wxFrame
54 class in the C++ version.
56 wxPython is very versatile. It can be used to create standalone GUI
57 applications, or in situations where Python is embedded in a C++ application as
58 an internal scripting or macro language.
60 Currently wxPython is available for Win32 platforms and the GTK toolkit (wxGTK)
61 on most Unix/X-windows platforms. See the wxPython website http://wxPython.org/
62 for details about getting wxPython working for you.
65 @section overview_python_why Why Use wxPython?
67 So why would you want to use wxPython over just C++ and wxWidgets? Personally I
68 prefer using Python for everything. I only use C++ when I absolutely have to
69 eke more performance out of an algorithm, and even then I usually code it as an
70 extension module and leave the majority of the program in Python.
72 Another good thing to use wxPython for is quick prototyping of your wxWidgets
73 apps. With C++ you have to continuously go though the edit-compile-link-run
74 cycle, which can be quite time consuming. With Python it is only an edit-run
75 cycle. You can easily build an application in a few hours with Python that
76 would normally take a few days or longer with C++. Converting a wxPython app to
77 a C++/wxWidgets app should be a straight forward task.
80 @section overview_python_othergui Other Python GUIs
82 There are other GUI solutions out there for Python.
84 @subsection overview_python_othergui_tkinter Tkinter
86 Tkinter is the de facto standard GUI for Python. It is available on nearly
87 every platform that Python and Tcl/TK are. Why Tcl/Tk? Well because Tkinter is
88 just a wrapper around Tcl's GUI toolkit, Tk. This has it's upsides and it's
91 The upside is that Tk is a pretty versatile toolkit. It can be made to do a lot
92 of things in a lot of different environments. It is fairly easy to create new
93 widgets and use them interchangeably in your programs.
95 The downside is Tcl. When using Tkinter you actually have two separate language
96 interpreters running, the Python interpreter and the Tcl interpreter for the
97 GUI. Since the guts of Tcl is mostly about string processing, it is fairly slow
98 as well. (Not too bad on a fast Pentium II, but you really notice the
99 difference on slower machines.)
101 It wasn't until the latest version of Tcl/Tk that native Look and Feel was
102 possible on non-Motif platforms. This is because Tk usually implements its own
103 widgets (controls) even when there are native controls available.
105 Tkinter is a pretty low-level toolkit. You have to do a lot of work (verbose
106 program code) to do things that would be much simpler with a higher level of
109 @subsection overview_python_othergui_pythonwin PythonWin
111 PythonWin is an add-on package for Python for the Win32 platform. It includes
112 wrappers for MFC as well as much of the Win32 API. Because of its foundation,
113 it is very familiar for programmers who have experience with MFC and the Win32
114 API. It is obviously not compatible with other platforms and toolkits.
115 PythonWin is organized as separate packages and modules so you can use the
116 pieces you need without having to use the GUI portions.
118 @subsection overview_python_othergui_others Others
120 There are quite a few other GUI modules available for Python, some in active
121 use, some that haven't been updated for ages. Most are simple wrappers around
122 some C or C++ toolkit or another, and most are not cross-platform compatible.
123 See http://pypi.python.org/pypi?:action=browse&show=all&c=433 for a listing of
127 @section overview_python_using Using wxPython
129 I'm not going to try and teach the Python language here. You can do that at the
130 <http://www.python.org/doc/tut/tut.html>. I'm also going to assume that you
131 know a bit about wxWidgets already, enough to notice the similarities in the
134 Take a look at the following wxPython program. You can find a similar program
135 in the wxPython/demo directory, named "DialogUnits.py". If your Python and
136 wxPython are properly installed, you should be able to run it by issuing this
140 python DialogUnits.py
144 01: ## import all of the wxPython GUI package
145 02: from wxPython.wx import *
147 04: ## Create a new frame class, derived from the wxPython Frame.
148 05: class MyFrame(wxFrame):
150 07: def __init__(self, parent, id, title):
151 08: # First, call the base class' __init__ method to create the frame
152 09: wxFrame.__init__(self, parent, id, title,
153 10: wxPoint(100, 100), wxSize(160, 100))
155 12: # Associate some events with methods of this class
156 13: EVT_SIZE(self, self.OnSize)
157 14: EVT_MOVE(self, self.OnMove)
159 16: # Add a panel and some controls to display the size and position
160 17: panel = wxPanel(self, -1)
161 18: wxStaticText(panel, -1, "Size:",
162 19: wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
163 20: wxStaticText(panel, -1, "Pos:",
164 21: wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
165 22: self.sizeCtrl = wxTextCtrl(panel, -1, "",
166 23: wxDLG_PNT(panel, wxPoint(24, 4)),
167 24: wxDLG_SZE(panel, wxSize(36, -1)),
169 26: self.posCtrl = wxTextCtrl(panel, -1, "",
170 27: wxDLG_PNT(panel, wxPoint(24, 14)),
171 28: wxDLG_SZE(panel, wxSize(36, -1)),
175 32: # This method is called automatically when the CLOSE event is
176 33: # sent to this window
177 34: def OnCloseWindow(self, event):
178 35: # tell the window to kill itself
181 38: # This method is called by the system when the window is resized,
182 39: # because of the association above.
183 40: def OnSize(self, event):
184 41: size = event.GetSize()
185 42: self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
187 44: # tell the event system to continue looking for an event handler,
188 45: # so the default handler will get called.
191 48: # This method is called by the system when the window is moved,
192 49: # because of the association above.
193 50: def OnMove(self, event):
194 51: pos = event.GetPosition()
195 52: self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
198 55: # Every wxWidgets application must have a class derived from wxApp
199 56: class MyApp(wxApp):
201 58: # wxWidgets calls this method to initialize the application
202 59: def OnInit(self):
204 61: # Create an instance of our customized Frame class
205 62: frame = MyFrame(NULL, -1, "This is a test")
208 65: # Tell wxWidgets that this is our main window
209 66: self.SetTopWindow(frame)
211 68: # Return a success flag
215 72: app = MyApp(0) # Create an instance of the application class
216 73: app.MainLoop() # Tell it to start processing events
220 @subsection overview_python_using_notice Things to Notice
222 At line 2 the wxPython classes, constants, and etc. are imported into the
223 current module's namespace. If you prefer to reduce namespace pollution you can
224 use "from wxPython import wx" and then access all the wxPython identifiers
225 through the wx module, for example, "wx.wxFrame".
227 At line 13 the frame's sizing and moving events are connected to methods of the
228 class. These helper functions are intended to be like the event table macros
229 that wxWidgets employs. But since static event tables are impossible with
230 wxPython, we use helpers that are named the same to dynamically build the
231 table. The only real difference is that the first argument to the event helpers
232 is always the window that the event table entry should be added to.
234 Notice the use of @c wxDLG_PNT and @c wxDLG_SZE in lines 19-29 to convert from
235 dialog units to pixels. These helpers are unique to wxPython since Python can't
236 do method overloading like C++.
238 There is an @c OnCloseWindow method at line 34 but no call to EVT_CLOSE to
239 attach the event to the method. Does it really get called? The answer is, yes
240 it does. This is because many of the standard events are attached to windows
241 that have the associated standard method names. I have tried to follow the lead
242 of the C++ classes in this area to determine what is standard but since that
243 changes from time to time I can make no guarantees, nor will it be fully
244 documented. When in doubt, use an EVT_*** function.
246 At lines 17 to 21 notice that there are no saved references to the panel or the
247 static text items that are created. Those of you who know Python might be
248 wondering what happens when Python deletes these objects when they go out of
249 scope. Do they disappear from the GUI? They don't. Remember that in wxPython
250 the Python objects are just shadows of the corresponding C++ objects. Once the
251 C++ windows and controls are attached to their parents, the parents manage them
252 and delete them when necessary. For this reason, most wxPython objects do not
253 need to have a __del__ method that explicitly causes the C++ object to be
254 deleted. If you ever have the need to forcibly delete a window, use the
255 Destroy() method as shown on line 36.
257 Just like wxWidgets in C++, wxPython apps need to create a class derived from
258 @c wxApp (line 56) that implements a method named @c OnInit, (line 59.) This
259 method should create the application's main window (line 62) and use
260 wxApp.SetTopWindow() (line 66) to inform wxWidgets about it.
262 And finally, at line 72 an instance of the application class is created. At
263 this point wxPython finishes initializing itself, and calls the @c OnInit
264 method to get things started. (The zero parameter here is a flag for
265 functionality that isn't quite implemented yet. Just ignore it for now.) The
266 call to @c MainLoop at line 73 starts the event loop which continues until the
267 application terminates or all the top level windows are closed.
270 @section overview_python_classes Classes Implemented in wxPython
272 The following classes are supported in wxPython. Most provide nearly full
273 implementations of the public interfaces specified in the C++ documentation,
274 others are less so. They will all be brought as close as possible to the C++
277 @li wxAcceleratorEntry
278 @li wxAcceleratorTable
282 @li wxBitmapDataObject
289 @li wxCalculateLayoutEvent
306 @li wxCustomDataObject
309 @li wxDataObjectComposite
310 @li wxDataObjectSimple
328 @li wxFileSystemHandler
339 @li wxHtmlContainerCell
341 @li wxHtmlEasyPrinting
347 @li wxHtmlWinTagHandler
355 @li wxIndividualLayoutConstraint
356 @li wxInitDialogEvent
358 @li wxInternetFSHandler
362 @li wxLayoutAlgorithm
363 @li wxLayoutConstraints
371 @li wxMDIClientWindow
374 @li wxMemoryFSHandler
386 @li wxPageSetupDialogData
387 @li wxPageSetupDialog
398 @li wxPrintDialogData
405 @li wxQueryLayoutInfoEvent
413 @li wxSashLayoutWindow
421 @li wxSingleChoiceDialog
436 @li wxSysColourChangedEvent
441 @li wxTextEntryDialog
460 @section overview_python_help Where to Go for Help
462 Since wxPython is a blending of multiple technologies, help comes from multiple
463 sources. See http://wxpython.org/ for details on various sources of help, but
464 probably the best source is the wxPython-users mail list. You can view the
465 archive or subscribe by going to http://wxpython.org/maillist.php
467 Or you can send mail directly to the list using this address:
468 wxpython-users@lists.wxwidgets.org