Fix a couple of spelling mistakes in the documentation.
[wxWidgets.git] / docs / doxygen / overviews / python.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: python.h
3 // Purpose: topic overview
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
7
8 /**
9
10 @page overview_python wxPython Overview
11
12 @tableofcontents
13
14 This topic was written by Robin Dunn, author of the
15 <a href="http://www.python.org/">wxPython</a> wrapper.
16
17 @section overview_python_what What is wxPython?
18
19 wxPython is a blending of the wxWidgets GUI classes and the Python programming
20 language.
21
22 @subsection overview_python_what_py Python
23
24 So what is Python? Go to http://www.python.org to learn more, but in a
25 nutshell Python is an interpreted, interactive, object-oriented programming
26 language. It is often compared to Tcl, Perl, Scheme or Java.
27
28 Python combines remarkable power with very clear syntax. It has modules,
29 classes, exceptions, very high level dynamic data types, and dynamic typing.
30 There are interfaces to many system calls and libraries, and new built-in
31 modules are easily written in C or C++. Python is also usable as an extension
32 language for applications that need a programmable interface.
33
34 Python is copyrighted but freely usable and distributable, even for commercial
35 use.
36
37 @subsection overview_python_what_wxpy wxPython
38
39 wxPython is a Python package that can be imported at runtime that includes a
40 collection of Python modules and an extension module (native code). It provides
41 a series of Python classes that mirror (or shadow) many of the wxWidgets GUI
42 classes. This extension module attempts to mirror the class hierarchy of
43 wxWidgets as closely as possible. This means that there is a wxFrame class in
44 wxPython that looks, smells, tastes and acts almost the same as the wxFrame
45 class in the C++ version.
46
47 wxPython is very versatile. It can be used to create standalone GUI
48 applications, or in situations where Python is embedded in a C++ application as
49 an internal scripting or macro language.
50
51 Currently wxPython is available for Win32 platforms and the GTK toolkit (wxGTK)
52 on most Unix/X-windows platforms. See the wxPython website http://wxPython.org/
53 for details about getting wxPython working for you.
54
55
56 @section overview_python_why Why Use wxPython?
57
58 So why would you want to use wxPython over just C++ and wxWidgets? Personally I
59 prefer using Python for everything. I only use C++ when I absolutely have to
60 eke more performance out of an algorithm, and even then I usually code it as an
61 extension module and leave the majority of the program in Python.
62
63 Another good thing to use wxPython for is quick prototyping of your wxWidgets
64 apps. With C++ you have to continuously go though the edit-compile-link-run
65 cycle, which can be quite time consuming. With Python it is only an edit-run
66 cycle. You can easily build an application in a few hours with Python that
67 would normally take a few days or longer with C++. Converting a wxPython app to
68 a C++/wxWidgets app should be a straight forward task.
69
70
71 @section overview_python_othergui Other Python GUIs
72
73 There are other GUI solutions out there for Python.
74
75 @subsection overview_python_othergui_tkinter Tkinter
76
77 Tkinter is the de facto standard GUI for Python. It is available on nearly
78 every platform that Python and Tcl/TK are. Why Tcl/Tk? Well because Tkinter is
79 just a wrapper around Tcl's GUI toolkit, Tk. This has it's upsides and it's
80 downsides...
81
82 The upside is that Tk is a pretty versatile toolkit. It can be made to do a lot
83 of things in a lot of different environments. It is fairly easy to create new
84 widgets and use them interchangeably in your programs.
85
86 The downside is Tcl. When using Tkinter you actually have two separate language
87 interpreters running, the Python interpreter and the Tcl interpreter for the
88 GUI. Since the guts of Tcl is mostly about string processing, it is fairly slow
89 as well. (Not too bad on a fast Pentium II, but you really notice the
90 difference on slower machines.)
91
92 It wasn't until the latest version of Tcl/Tk that native Look and Feel was
93 possible on non-Motif platforms. This is because Tk usually implements its own
94 widgets (controls) even when there are native controls available.
95
96 Tkinter is a pretty low-level toolkit. You have to do a lot of work (verbose
97 program code) to do things that would be much simpler with a higher level of
98 abstraction.
99
100 @subsection overview_python_othergui_pythonwin PythonWin
101
102 PythonWin is an add-on package for Python for the Win32 platform. It includes
103 wrappers for MFC as well as much of the Win32 API. Because of its foundation,
104 it is very familiar for programmers who have experience with MFC and the Win32
105 API. It is obviously not compatible with other platforms and toolkits.
106 PythonWin is organized as separate packages and modules so you can use the
107 pieces you need without having to use the GUI portions.
108
109 @subsection overview_python_othergui_others Others
110
111 There are quite a few other GUI modules available for Python, some in active
112 use, some that haven't been updated for ages. Most are simple wrappers around
113 some C or C++ toolkit or another, and most are not cross-platform compatible.
114 See <a href="http://pypi.python.org/pypi?:action=browse&show=all&c=433">this link</a>
115 for a listing of a few of them.
116
117
118 @section overview_python_using Using wxPython
119
120 I'm not going to try and teach the Python language here. You can do that at the
121 <a href="http://www.python.org/doc/tut/tut.html">Python Tutorial</a>. I'm also
122 going to assume that you know a bit about wxWidgets already, enough to notice
123 the similarities in the classes used.
124
125 Take a look at the following wxPython program. You can find a similar program
126 in the @c wxPython/demo directory, named @c DialogUnits.py. If your Python and
127 wxPython are properly installed, you should be able to run it by issuing this
128 command:
129
130 @code
131 python DialogUnits.py
132 @endcode
133
134 @code
135 01: ## import all of the wxPython GUI package
136 02: from wxPython.wx import *
137 03:
138 04: ## Create a new frame class, derived from the wxPython Frame.
139 05: class MyFrame(wxFrame):
140 06:
141 07: def __init__(self, parent, id, title):
142 08: # First, call the base class' __init__ method to create the frame
143 09: wxFrame.__init__(self, parent, id, title,
144 10: wxPoint(100, 100), wxSize(160, 100))
145 11:
146 12: # Associate some events with methods of this class
147 13: EVT_SIZE(self, self.OnSize)
148 14: EVT_MOVE(self, self.OnMove)
149 15:
150 16: # Add a panel and some controls to display the size and position
151 17: panel = wxPanel(self, -1)
152 18: wxStaticText(panel, -1, "Size:",
153 19: wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
154 20: wxStaticText(panel, -1, "Pos:",
155 21: wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
156 22: self.sizeCtrl = wxTextCtrl(panel, -1, "",
157 23: wxDLG_PNT(panel, wxPoint(24, 4)),
158 24: wxDLG_SZE(panel, wxSize(36, -1)),
159 25: wxTE_READONLY)
160 26: self.posCtrl = wxTextCtrl(panel, -1, "",
161 27: wxDLG_PNT(panel, wxPoint(24, 14)),
162 28: wxDLG_SZE(panel, wxSize(36, -1)),
163 29: wxTE_READONLY)
164 30:
165 31:
166 32: # This method is called automatically when the CLOSE event is
167 33: # sent to this window
168 34: def OnCloseWindow(self, event):
169 35: # tell the window to kill itself
170 36: self.Destroy()
171 37:
172 38: # This method is called by the system when the window is resized,
173 39: # because of the association above.
174 40: def OnSize(self, event):
175 41: size = event.GetSize()
176 42: self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
177 43:
178 44: # tell the event system to continue looking for an event handler,
179 45: # so the default handler will get called.
180 46: event.Skip()
181 47:
182 48: # This method is called by the system when the window is moved,
183 49: # because of the association above.
184 50: def OnMove(self, event):
185 51: pos = event.GetPosition()
186 52: self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
187 53:
188 54:
189 55: # Every wxWidgets application must have a class derived from wxApp
190 56: class MyApp(wxApp):
191 57:
192 58: # wxWidgets calls this method to initialize the application
193 59: def OnInit(self):
194 60:
195 61: # Create an instance of our customized Frame class
196 62: frame = MyFrame(NULL, -1, "This is a test")
197 63: frame.Show(true)
198 64:
199 67:
200 68: # Return a success flag
201 69: return true
202 70:
203 71:
204 72: app = MyApp(0) # Create an instance of the application class
205 73: app.MainLoop() # Tell it to start processing events
206 74:
207 @endcode
208
209 @subsection overview_python_using_notice Things to Notice
210
211 At line 2 the wxPython classes, constants, and etc. are imported into the
212 current module's namespace. If you prefer to reduce namespace pollution you can
213 use @c "from wxPython import wx" and then access all the wxPython identifiers
214 through the wx module, for example, @c "wx.wxFrame".
215
216 At line 13 the frame's sizing and moving events are connected to methods of the
217 class. These helper functions are intended to be like the event table macros
218 that wxWidgets employs. But since static event tables are impossible with
219 wxPython, we use helpers that are named the same to dynamically build the
220 table. The only real difference is that the first argument to the event helpers
221 is always the window that the event table entry should be added to.
222
223 Notice the use of @c wxDLG_PNT and @c wxDLG_SZE in lines 19-29 to convert from
224 dialog units to pixels. These helpers are unique to wxPython since Python can't
225 do method overloading like C++.
226
227 There is an @c OnCloseWindow method at line 34 but no call to @c EVT_CLOSE to
228 attach the event to the method. Does it really get called? The answer is, yes
229 it does. This is because many of the standard events are attached to windows
230 that have the associated standard method names. I have tried to follow the lead
231 of the C++ classes in this area to determine what is standard but since that
232 changes from time to time I can make no guarantees, nor will it be fully
233 documented. When in doubt, use an @c EVT_*** function.
234
235 At lines 17 to 21 notice that there are no saved references to the panel or the
236 static text items that are created. Those of you who know Python might be
237 wondering what happens when Python deletes these objects when they go out of
238 scope. Do they disappear from the GUI? They don't. Remember that in wxPython
239 the Python objects are just shadows of the corresponding C++ objects. Once the
240 C++ windows and controls are attached to their parents, the parents manage them
241 and delete them when necessary. For this reason, most wxPython objects do not
242 need to have a @c __del__ method that explicitly causes the C++ object to be
243 deleted. If you ever have the need to forcibly delete a window, use the
244 Destroy() method as shown on line 36.
245
246 Just like wxWidgets in C++, wxPython apps need to create a class derived from
247 @c wxApp (line 56) that implements a method named @c OnInit, (line 59.) This
248 method should create the application's main window (line 62) and show it.
249
250 And finally, at line 72 an instance of the application class is created. At
251 this point wxPython finishes initializing itself, and calls the @c OnInit
252 method to get things started. (The zero parameter here is a flag for
253 functionality that isn't quite implemented yet. Just ignore it for now.) The
254 call to @c MainLoop at line 73 starts the event loop which continues until the
255 application terminates or all the top level windows are closed.
256
257
258 @section overview_python_classes Classes Implemented in wxPython
259
260 The following classes are supported in wxPython. Most provide nearly full
261 implementations of the public interfaces specified in the C++ documentation,
262 others are less so. They will all be brought as close as possible to the C++
263 spec over time.
264
265 @li wxAcceleratorEntry
266 @li wxAcceleratorTable
267 @li wxActivateEvent
268 @li wxBitmap
269 @li wxBitmapButton
270 @li wxBitmapDataObject
271 @li wxBMPHandler
272 @li wxBoxSizer
273 @li wxBrush
274 @li wxBusyInfo
275 @li wxBusyCursor
276 @li wxButton
277 @li wxCalculateLayoutEvent
278 @li wxCalendarCtrl
279 @li wxCaret
280 @li wxCheckBox
281 @li wxCheckListBox
282 @li wxChoice
283 @li wxClientDC
284 @li wxClipboard
285 @li wxCloseEvent
286 @li wxColourData
287 @li wxColourDialog
288 @li wxColour
289 @li wxComboBox
290 @li wxCommandEvent
291 @li wxConfigBase
292 @li wxControl
293 @li wxCursor
294 @li wxCustomDataObject
295 @li wxDataFormat
296 @li wxDataObject
297 @li wxDataObjectComposite
298 @li wxDataObjectSimple
299 @li wxDateTime
300 @li wxDateSpan
301 @li wxDC
302 @li wxDialog
303 @li wxDirDialog
304 @li wxDragImage
305 @li wxDropFilesEvent
306 @li wxDropSource
307 @li wxDropTarget
308 @li wxEraseEvent
309 @li wxEvent
310 @li wxEvtHandler
311 @li wxFileConfig
312 @li wxFileDataObject
313 @li wxFileDialog
314 @li wxFileDropTarget
315 @li wxFileSystem
316 @li wxFileSystemHandler
317 @li wxFocusEvent
318 @li wxFontData
319 @li wxFontDialog
320 @li wxFont
321 @li wxFrame
322 @li wxFSFile
323 @li wxGauge
324 @li wxGIFHandler
325 @li wxGLCanvas
326 @li wxHtmlCell
327 @li wxHtmlContainerCell
328 @li wxHtmlDCRenderer
329 @li wxHtmlEasyPrinting
330 @li wxHtmlParser
331 @li wxHtmlTagHandler
332 @li wxHtmlTag
333 @li wxHtmlWinParser
334 @li wxHtmlPrintout
335 @li wxHtmlWinTagHandler
336 @li wxHtmlWindow
337 @li wxIconizeEvent
338 @li wxIcon
339 @li wxIdleEvent
340 @li wxImage
341 @li wxImageHandler
342 @li wxImageList
343 @li wxIndividualLayoutConstraint
344 @li wxInitDialogEvent
345 @li wxInputStream
346 @li @ref wxFileSystem "wxInternetFSHandler"
347 @li wxJoystickEvent
348 @li wxJPEGHandler
349 @li wxKeyEvent
350 @li wxLayoutAlgorithm
351 @li wxLayoutConstraints
352 @li wxListBox
353 @li wxListCtrl
354 @li wxListEvent
355 @li wxListItem
356 @li wxMask
357 @li wxMaximizeEvent
358 @li wxMDIChildFrame
359 @li wxMDIClientWindow
360 @li wxMDIParentFrame
361 @li wxMemoryDC
362 @li wxMemoryFSHandler
363 @li wxMenuBar
364 @li wxMenuEvent
365 @li wxMenuItem
366 @li wxMenu
367 @li wxMessageDialog
368 @li wxMetafileDC
369 @li wxMiniFrame
370 @li wxMouseEvent
371 @li wxMoveEvent
372 @li wxNotebookEvent
373 @li wxNotebook
374 @li wxPageSetupDialogData
375 @li wxPageSetupDialog
376 @li wxPaintDC
377 @li wxPaintEvent
378 @li wxPalette
379 @li wxPanel
380 @li wxPen
381 @li wxPNGHandler
382 @li wxPoint
383 @li wxPostScriptDC
384 @li wxPreviewFrame
385 @li wxPrintData
386 @li wxPrintDialogData
387 @li wxPrintDialog
388 @li wxPrinter
389 @li wxPrintPreview
390 @li wxPrinterDC
391 @li wxPrintout
392 @li wxProcess
393 @li wxQueryLayoutInfoEvent
394 @li wxRadioBox
395 @li wxRadioButton
396 @li wxRealPoint
397 @li wxRect
398 @li wxRegionIterator
399 @li wxRegion
400 @li wxSashEvent
401 @li wxSashLayoutWindow
402 @li wxSashWindow
403 @li wxScreenDC
404 @li wxScrollBar
405 @li wxScrollEvent
406 @li ::wxScrolledWindow
407 @li wxScrollWinEvent
408 @li wxShowEvent
409 @li wxSingleChoiceDialog
410 @li wxSizeEvent
411 @li wxSize
412 @li wxSizer
413 @li wxSizerItem
414 @li wxSlider
415 @li wxSpinButton
416 @li wxSpinEvent
417 @li wxSplitterWindow
418 @li wxStaticBitmap
419 @li wxStaticBox
420 @li wxStaticBoxSizer
421 @li wxStaticLine
422 @li wxStaticText
423 @li wxStatusBar
424 @li wxSysColourChangedEvent
425 @li wxTaskBarIcon
426 @li wxTextCtrl
427 @li wxTextDataObject
428 @li wxTextDropTarget
429 @li wxTextEntryDialog
430 @li wxTimer
431 @li wxTimerEvent
432 @li wxTimeSpan
433 @li wxTipProvider
434 @li wxToolBarTool
435 @li wxToolBar
436 @li wxToolTip
437 @li wxTreeCtrl
438 @li wxTreeEvent
439 @li wxTreeItemData
440 @li wxTreeItemId
441 @li wxUpdateUIEvent
442 @li wxValidator
443 @li wxWindowDC
444 @li wxWindow
445 @li @ref wxFileSystem "wxZipFSHandler"
446
447
448 @section overview_python_help Where to Go for Help
449
450 Since wxPython is a blending of multiple technologies, help comes from multiple
451 sources. See http://wxpython.org/ for details on various sources of help, but
452 probably the best source is the wxPython-users mail list. You can view the
453 archive or subscribe by going to http://wxpython.org/maillist.php
454
455 Or you can send mail directly to the list using this address:
456 wxpython-users@lists.wxwidgets.org
457
458 */