]> git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/python.h
added wxWidgets samples
[wxWidgets.git] / docs / doxygen / overviews / python.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: python
3 // Purpose: topic overview
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /*!
10
11 @page python_overview wxPython overview
12
13 This topic was written by Robin Dunn, author of the wxPython wrapper.
14 @ref pwhat_overview
15 @ref pwhy_overview
16 @ref pother_overview
17 @ref pusing_overview
18 @ref pclasses_overview
19 @ref phelp_overview
20
21
22 @section wxpwhat What is wxPython?
23
24 wxPython is a blending of the wxWidgets GUI classes and the
25 #Python programming language.
26 @b Python
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
39 commercial use.
40 @b wxPython
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
48 the C++ version.
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.
56
57 @section wxpwhy Why use wxPython?
58
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.
71
72 @section wxpother Other Python GUIs
73
74 There are other GUI solutions out there for Python.
75 @b Tkinter
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
83 programs.
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
95 level of abstraction.
96 @b PythonWin
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.
104 @b Others
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.
110
111 @section wxpusing Using wxPython
112
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:
122
123
124 @b @c python DialogUnits.py
125
126
127
128
129
130 @code
131 001: ## import all of the wxPython GUI package
132 002: from wxPython.wx import *
133 003:
134 004: ## Create a new frame class, derived from the wxPython Frame.
135 005: class MyFrame(wxFrame):
136 006:
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))
141 011:
142 012: # Associate some events with methods of this class
143 013: EVT_SIZE(self, self.OnSize)
144 014: EVT_MOVE(self, self.OnMove)
145 015:
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)),
155 025: wxTE_READONLY)
156 026: self.posCtrl = wxTextCtrl(panel, -1, "",
157 027: wxDLG_PNT(panel, wxPoint(24, 14)),
158 028: wxDLG_SZE(panel, wxSize(36, -1)),
159 029: wxTE_READONLY)
160 030:
161 031:
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
166 036: self.Destroy()
167 037:
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))
173 043:
174 044: # tell the event system to continue looking for an event handler,
175 045: # so the default handler will get called.
176 046: event.Skip()
177 047:
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))
183 053:
184 054:
185 055: # Every wxWidgets application must have a class derived from wxApp
186 056: class MyApp(wxApp):
187 057:
188 058: # wxWidgets calls this method to initialize the application
189 059: def OnInit(self):
190 060:
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)
194 064:
195 065: # Tell wxWidgets that this is our main window
196 066: self.SetTopWindow(frame)
197 067:
198 068: # Return a success flag
199 069: return @true
200 070:
201 071:
202 072: app = MyApp(0) # Create an instance of the application class
203 073: app.MainLoop() # Tell it to start processing events
204 074:
205 @endcode
206
207
208
209 @b Things to notice
210
211
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.
258
259
260
261 @section wxpclasses wxWidgets classes implemented in wxPython
262
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.
267
268
269 #wxAcceleratorEntry
270 #wxAcceleratorTable
271 #wxActivateEvent
272 #wxBitmap
273 #wxBitmapButton
274 #wxBitmapDataObject
275 wxBMPHandler
276 #wxBoxSizer
277 #wxBrush
278 #wxBusyInfo
279 #wxBusyCursor
280 #wxButton
281 #wxCalculateLayoutEvent
282 #wxCalendarCtrl
283 #wxCaret
284 #wxCheckBox
285 #wxCheckListBox
286 #wxChoice
287 #wxClientDC
288 #wxClipboard
289 #wxCloseEvent
290 #wxColourData
291 #wxColourDialog
292 #wxColour
293 #wxComboBox
294 #wxCommandEvent
295 #wxConfig
296 #wxControl
297 #wxCursor
298 #wxCustomDataObject
299 #wxDataFormat
300 #wxDataObject
301 #wxDataObjectComposite
302 #wxDataObjectSimple
303 #wxDateTime
304 #wxDateSpan
305 #wxDC
306 #wxDialog
307 #wxDirDialog
308 #wxDragImage
309 #wxDropFilesEvent
310 #wxDropSource
311 #wxDropTarget
312 #wxEraseEvent
313 #wxEvent
314 #wxEvtHandler
315 #wxFileConfig
316 #wxFileDataObject
317 #wxFileDialog
318 #wxFileDropTarget
319 #wxFileSystem
320 #wxFileSystemHandler
321 #wxFocusEvent
322 #wxFontData
323 #wxFontDialog
324 #wxFont
325 #wxFrame
326 #wxFSFile
327 #wxGauge
328 wxGIFHandler
329 #wxGLCanvas
330 #wxHtmlCell
331 #wxHtmlContainerCell
332 #wxHtmlDCRenderer
333 #wxHtmlEasyPrinting
334 #wxHtmlParser
335 #wxHtmlTagHandler
336 #wxHtmlTag
337 #wxHtmlWinParser
338 #wxHtmlPrintout
339 #wxHtmlWinTagHandler
340 #wxHtmlWindow
341 #wxIconizeEvent
342 #wxIcon
343 #wxIdleEvent
344 #wxImage
345 #wxImageHandler
346 #wxImageList
347 #wxIndividualLayoutConstraint
348 #wxInitDialogEvent
349 #wxInputStream
350 #wxInternetFSHandler
351 #wxJoystickEvent
352 wxJPEGHandler
353 #wxKeyEvent
354 #wxLayoutAlgorithm
355 #wxLayoutConstraints
356 #wxListBox
357 #wxListCtrl
358 #wxListEvent
359 #wxListItem
360 #wxMask
361 #wxMaximizeEvent
362 #wxMDIChildFrame
363 #wxMDIClientWindow
364 #wxMDIParentFrame
365 #wxMemoryDC
366 #wxMemoryFSHandler
367 #wxMenuBar
368 #wxMenuEvent
369 #wxMenuItem
370 #wxMenu
371 #wxMessageDialog
372 #wxMetaFileDC
373 #wxMiniFrame
374 #wxMouseEvent
375 #wxMoveEvent
376 #wxNotebookEvent
377 #wxNotebook
378 #wxPageSetupDialogData
379 #wxPageSetupDialog
380 #wxPaintDC
381 #wxPaintEvent
382 #wxPalette
383 #wxPanel
384 #wxPen
385 wxPNGHandler
386 #wxPoint
387 #wxPostScriptDC
388 #wxPreviewFrame
389 #wxPrintData
390 #wxPrintDialogData
391 #wxPrintDialog
392 #wxPrinter
393 #wxPrintPreview
394 #wxPrinterDC
395 #wxPrintout
396 #wxProcess
397 #wxQueryLayoutInfoEvent
398 #wxRadioBox
399 #wxRadioButton
400 #wxRealPoint
401 #wxRect
402 #wxRegionIterator
403 #wxRegion
404 #wxSashEvent
405 #wxSashLayoutWindow
406 #wxSashWindow
407 #wxScreenDC
408 #wxScrollBar
409 #wxScrollEvent
410 #wxScrolledWindow
411 #wxScrollWinEvent
412 wxShowEvent
413 #wxSingleChoiceDialog
414 #wxSizeEvent
415 #wxSize
416 #wxSizer
417 #wxSizerItem
418 #wxSlider
419 #wxSpinButton
420 #wxSpinEvent
421 #wxSplitterWindow
422 #wxStaticBitmap
423 #wxStaticBox
424 #wxStaticBoxSizer
425 #wxStaticLine
426 #wxStaticText
427 #wxStatusBar
428 #wxSysColourChangedEvent
429 #wxTaskBarIcon
430 #wxTextCtrl
431 #wxTextDataObject
432 #wxTextDropTarget
433 #wxTextEntryDialog
434 #wxTimer
435 #wxTimerEvent
436 #wxTimeSpan
437 #wxTipProvider
438 wxToolBarTool
439 #wxToolBar
440 #wxToolTip
441 #wxTreeCtrl
442 #wxTreeEvent
443 #wxTreeItemData
444 wxTreeItemId
445 #wxUpdateUIEvent
446 #wxValidator
447 #wxWindowDC
448 #wxWindow
449 #wxZipFSHandler
450
451
452
453 @section wxphelp Where to go for help
454
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
460 going to
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
464
465 */
466
467