]>
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 python_overview wxPython overview
13 This topic was written by Robin Dunn, author of the wxPython wrapper.
18 @ref pclasses_overview
22 @section wxpwhat What is wxPython?
24 wxPython is a blending of the wxWidgets GUI classes and the
25 #Python programming language.
27 So what is Python? Go to
28 #http://www.python.org to learn more,
29 but in a nutshell Python is an interpreted,
30 interactive, object-oriented programming language. It is often
31 compared to Tcl, Perl, Scheme or Java.
32 Python combines remarkable power with very clear syntax. It has
33 modules, classes, exceptions, very high level dynamic data types, and
34 dynamic typing. There are interfaces to many system calls and
35 libraries, and new built-in modules are easily written in C or
36 C++. Python is also usable as an extension language for applications
37 that need a programmable interface.
38 Python is copyrighted but freely usable and distributable, even for
41 wxPython is a Python package that can be imported at runtime that
42 includes a collection of Python modules and an extension module
43 (native code). It provides a series of Python classes that mirror (or
44 shadow) many of the wxWidgets GUI classes. This extension module
45 attempts to mirror the class hierarchy of wxWidgets as closely as
46 possible. This means that there is a wxFrame class in wxPython that
47 looks, smells, tastes and acts almost the same as the wxFrame class in
49 wxPython is very versatile. It can be used to create standalone GUI
50 applications, or in situations where Python is embedded in a C++
51 application as an internal scripting or macro language.
52 Currently wxPython is available for Win32 platforms and the GTK
53 toolkit (wxGTK) on most Unix/X-windows platforms. See the wxPython
54 website #http://wxPython.org/ for
55 details about getting wxPython working for you.
57 @section wxpwhy Why use wxPython?
59 So why would you want to use wxPython over just C++ and wxWidgets?
60 Personally I prefer using Python for everything. I only use C++ when I
61 absolutely have to eke more performance out of an algorithm, and even
62 then I usually code it as an extension module and leave the majority
63 of the program in Python.
64 Another good thing to use wxPython for is quick prototyping of your
65 wxWidgets apps. With C++ you have to continuously go though the
66 edit-compile-link-run cycle, which can be quite time consuming. With
67 Python it is only an edit-run cycle. You can easily build an
68 application in a few hours with Python that would normally take a few
69 days or longer with C++. Converting a wxPython app to a C++/wxWidgets app
70 should be a straight forward task.
72 @section wxpother Other Python GUIs
74 There are other GUI solutions out there for Python.
76 Tkinter is the de facto standard GUI for Python. It is available
77 on nearly every platform that Python and Tcl/TK are. Why Tcl/Tk?
78 Well because Tkinter is just a wrapper around Tcl's GUI toolkit, Tk.
79 This has its upsides and its downsides...
80 The upside is that Tk is a pretty versatile toolkit. It can be made
81 to do a lot of things in a lot of different environments. It is fairly
82 easy to create new widgets and use them interchangeably in your
84 The downside is Tcl. When using Tkinter you actually have two
85 separate language interpreters running, the Python interpreter and the
86 Tcl interpreter for the GUI. Since the guts of Tcl is mostly about
87 string processing, it is fairly slow as well. (Not too bad on a fast
88 Pentium II, but you really notice the difference on slower machines.)
89 It wasn't until the latest version of Tcl/Tk that native Look and
90 Feel was possible on non-Motif platforms. This is because Tk
91 usually implements its own widgets (controls) even when there are
92 native controls available.
93 Tkinter is a pretty low-level toolkit. You have to do a lot of work
94 (verbose program code) to do things that would be much simpler with a higher
97 PythonWin is an add-on package for Python for the Win32 platform. It
98 includes wrappers for MFC as well as much of the Win32 API. Because
99 of its foundation, it is very familiar for programmers who have
100 experience with MFC and the Win32 API. It is obviously not compatible
101 with other platforms and toolkits. PythonWin is organized as separate
102 packages and modules so you can use the pieces you need without having
103 to use the GUI portions.
105 There are quite a few other GUI modules available for Python, some in
106 active use, some that haven't been updated for ages. Most are simple
107 wrappers around some C or C++ toolkit or another, and most are not
108 cross-platform compatible. See @ref Graphics_overview
109 for a listing of a few of them.
111 @section wxpusing Using wxPython
113 @b First things first...
114 I'm not going to try and teach the Python language here. You can do
115 that at the http://www.python.org/doc/tut/tut.html.
116 I'm also going to assume that you know a bit about wxWidgets already,
117 enough to notice the similarities in the classes used.
118 Take a look at the following wxPython program. You can find a similar
119 program in the @c wxPython/demo directory, named @c DialogUnits.py. If your
120 Python and wxPython are properly installed, you should be able to run
121 it by issuing this command:
124 @b @c python DialogUnits.py
131 001: ## import all of the wxPython GUI package
132 002: from wxPython.wx import *
134 004: ## Create a new frame class, derived from the wxPython Frame.
135 005: class MyFrame(wxFrame):
137 007: def __init__(self, parent, id, title):
138 008: # First, call the base class' __init__ method to create the frame
139 009: wxFrame.__init__(self, parent, id, title,
140 010: wxPoint(100, 100), wxSize(160, 100))
142 012: # Associate some events with methods of this class
143 013: EVT_SIZE(self, self.OnSize)
144 014: EVT_MOVE(self, self.OnMove)
146 016: # Add a panel and some controls to display the size and position
147 017: panel = wxPanel(self, -1)
148 018: wxStaticText(panel, -1, "Size:",
149 019: wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
150 020: wxStaticText(panel, -1, "Pos:",
151 021: wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
152 022: self.sizeCtrl = wxTextCtrl(panel, -1, "",
153 023: wxDLG_PNT(panel, wxPoint(24, 4)),
154 024: wxDLG_SZE(panel, wxSize(36, -1)),
156 026: self.posCtrl = wxTextCtrl(panel, -1, "",
157 027: wxDLG_PNT(panel, wxPoint(24, 14)),
158 028: wxDLG_SZE(panel, wxSize(36, -1)),
162 032: # This method is called automatically when the CLOSE event is
163 033: # sent to this window
164 034: def OnCloseWindow(self, event):
165 035: # tell the window to kill itself
168 038: # This method is called by the system when the window is resized,
169 039: # because of the association above.
170 040: def OnSize(self, event):
171 041: size = event.GetSize()
172 042: self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
174 044: # tell the event system to continue looking for an event handler,
175 045: # so the default handler will get called.
178 048: # This method is called by the system when the window is moved,
179 049: # because of the association above.
180 050: def OnMove(self, event):
181 051: pos = event.GetPosition()
182 052: self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
185 055: # Every wxWidgets application must have a class derived from wxApp
186 056: class MyApp(wxApp):
188 058: # wxWidgets calls this method to initialize the application
189 059: def OnInit(self):
191 061: # Create an instance of our customized Frame class
192 062: frame = MyFrame(@NULL, -1, "This is a test")
193 063: frame.Show(@true)
195 065: # Tell wxWidgets that this is our main window
196 066: self.SetTopWindow(frame)
198 068: # Return a success flag
202 072: app = MyApp(0) # Create an instance of the application class
203 073: app.MainLoop() # Tell it to start processing events
212 At line 2 the wxPython classes, constants, and etc. are imported
213 into the current module's namespace. If you prefer to reduce
214 namespace pollution you can use "@c from wxPython import wx" and
215 then access all the wxPython identifiers through the wx module, for
216 example, "@c wx.wxFrame".
217 At line 13 the frame's sizing and moving events are connected to
218 methods of the class. These helper functions are intended to be like
219 the event table macros that wxWidgets employs. But since static event
220 tables are impossible with wxPython, we use helpers that are named the
221 same to dynamically build the table. The only real difference is
222 that the first argument to the event helpers is always the window that
223 the event table entry should be added to.
224 Notice the use of @c wxDLG_PNT and @c wxDLG_SZE in lines 19
225 - 29 to convert from dialog units to pixels. These helpers are unique
226 to wxPython since Python can't do method overloading like C++.
227 There is an @c OnCloseWindow method at line 34 but no call to
228 EVT_CLOSE to attach the event to the method. Does it really get
229 called? The answer is, yes it does. This is because many of the
230 standard events are attached to windows that have the associated
231 standard method names. I have tried to follow the lead of the
232 C++ classes in this area to determine what is standard but since
233 that changes from time to time I can make no guarantees, nor will it
234 be fully documented. When in doubt, use an EVT_*** function.
235 At lines 17 to 21 notice that there are no saved references to
236 the panel or the static text items that are created. Those of you
237 who know Python might be wondering what happens when Python deletes
238 these objects when they go out of scope. Do they disappear from the GUI? They
239 don't. Remember that in wxPython the Python objects are just shadows of the
240 corresponding C++ objects. Once the C++ windows and controls are
241 attached to their parents, the parents manage them and delete them
242 when necessary. For this reason, most wxPython objects do not need to
243 have a __del__ method that explicitly causes the C++ object to be
244 deleted. If you ever have the need to forcibly delete a window, use
245 the Destroy() method as shown on line 36.
246 Just like wxWidgets in C++, wxPython apps need to create a class
247 derived from @c wxApp (line 56) that implements a method named
248 @c OnInit, (line 59.) This method should create the application's
249 main window (line 62) and use @c wxApp.SetTopWindow() (line 66) to
250 inform wxWidgets about it.
251 And finally, at line 72 an instance of the application class is
252 created. At this point wxPython finishes initializing itself, and calls
253 the @c OnInit method to get things started. (The zero parameter here is
254 a flag for functionality that isn't quite implemented yet. Just
255 ignore it for now.) The call to @c MainLoop at line 73 starts the event
256 loop which continues until the application terminates or all the top
257 level windows are closed.
261 @section wxpclasses wxWidgets classes implemented in wxPython
263 The following classes are supported in wxPython. Most provide nearly
264 full implementations of the public interfaces specified in the C++
265 documentation, others are less so. They will all be brought as close
266 as possible to the C++ spec over time.
281 #wxCalculateLayoutEvent
301 #wxDataObjectComposite
347 #wxIndividualLayoutConstraint
378 #wxPageSetupDialogData
397 #wxQueryLayoutInfoEvent
413 #wxSingleChoiceDialog
428 #wxSysColourChangedEvent
453 @section wxphelp Where to go for help
455 Since wxPython is a blending of multiple technologies, help comes from
456 multiple sources. See
457 #http://wxpython.org/ for details on
458 various sources of help, but probably the best source is the
459 wxPython-users mail list. You can view the archive or subscribe by
461 #http://lists.wxwindows.org/mailman/listinfo/wxpython-users
462 Or you can send mail directly to the list using this address:
463 wxpython-users@lists.wxwindows.org