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