]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/python.h
Revert "Make wxMSW stack walking methods work with Unicode identifiers."
[wxWidgets.git] / docs / doxygen / overviews / python.h
CommitLineData
15b6757b 1/////////////////////////////////////////////////////////////////////////////
2cd3cc94 2// Name: python.h
15b6757b
FM
3// Purpose: topic overview
4// Author: wxWidgets team
526954c5 5// Licence: wxWindows licence
15b6757b
FM
6/////////////////////////////////////////////////////////////////////////////
7
880efa2a 8/**
36c9828f 9
2cd3cc94 10@page overview_python wxPython Overview
36c9828f 11
831e1028
BP
12@tableofcontents
13
3ed3a1c8
BP
14This topic was written by Robin Dunn, author of the
15<a href="http://www.python.org/">wxPython</a> wrapper.
2cd3cc94 16
2cd3cc94
BP
17@section overview_python_what What is wxPython?
18
19wxPython is a blending of the wxWidgets GUI classes and the Python programming
20language.
21
22@subsection overview_python_what_py Python
23
24So what is Python? Go to http://www.python.org to learn more, but in a
25nutshell Python is an interpreted, interactive, object-oriented programming
26language. It is often compared to Tcl, Perl, Scheme or Java.
27
28Python combines remarkable power with very clear syntax. It has modules,
29classes, exceptions, very high level dynamic data types, and dynamic typing.
30There are interfaces to many system calls and libraries, and new built-in
31modules are easily written in C or C++. Python is also usable as an extension
32language for applications that need a programmable interface.
33
34Python is copyrighted but freely usable and distributable, even for commercial
35use.
36
37@subsection overview_python_what_wxpy wxPython
38
39wxPython is a Python package that can be imported at runtime that includes a
40collection of Python modules and an extension module (native code). It provides
41a series of Python classes that mirror (or shadow) many of the wxWidgets GUI
42classes. This extension module attempts to mirror the class hierarchy of
43wxWidgets as closely as possible. This means that there is a wxFrame class in
44wxPython that looks, smells, tastes and acts almost the same as the wxFrame
45class in the C++ version.
46
47wxPython is very versatile. It can be used to create standalone GUI
48applications, or in situations where Python is embedded in a C++ application as
49an internal scripting or macro language.
50
51Currently wxPython is available for Win32 platforms and the GTK toolkit (wxGTK)
52on most Unix/X-windows platforms. See the wxPython website http://wxPython.org/
53for details about getting wxPython working for you.
54
55
56@section overview_python_why Why Use wxPython?
57
58So why would you want to use wxPython over just C++ and wxWidgets? Personally I
59prefer using Python for everything. I only use C++ when I absolutely have to
60eke more performance out of an algorithm, and even then I usually code it as an
61extension module and leave the majority of the program in Python.
62
63Another good thing to use wxPython for is quick prototyping of your wxWidgets
64apps. With C++ you have to continuously go though the edit-compile-link-run
65cycle, which can be quite time consuming. With Python it is only an edit-run
66cycle. You can easily build an application in a few hours with Python that
67would normally take a few days or longer with C++. Converting a wxPython app to
68a C++/wxWidgets app should be a straight forward task.
69
70
71@section overview_python_othergui Other Python GUIs
72
73There are other GUI solutions out there for Python.
74
75@subsection overview_python_othergui_tkinter Tkinter
76
77Tkinter is the de facto standard GUI for Python. It is available on nearly
78every platform that Python and Tcl/TK are. Why Tcl/Tk? Well because Tkinter is
79just a wrapper around Tcl's GUI toolkit, Tk. This has it's upsides and it's
80downsides...
81
82The upside is that Tk is a pretty versatile toolkit. It can be made to do a lot
83of things in a lot of different environments. It is fairly easy to create new
84widgets and use them interchangeably in your programs.
85
86The downside is Tcl. When using Tkinter you actually have two separate language
87interpreters running, the Python interpreter and the Tcl interpreter for the
88GUI. Since the guts of Tcl is mostly about string processing, it is fairly slow
89as well. (Not too bad on a fast Pentium II, but you really notice the
90difference on slower machines.)
91
92It wasn't until the latest version of Tcl/Tk that native Look and Feel was
93possible on non-Motif platforms. This is because Tk usually implements its own
94widgets (controls) even when there are native controls available.
95
96Tkinter is a pretty low-level toolkit. You have to do a lot of work (verbose
97program code) to do things that would be much simpler with a higher level of
98abstraction.
99
100@subsection overview_python_othergui_pythonwin PythonWin
101
102PythonWin is an add-on package for Python for the Win32 platform. It includes
103wrappers for MFC as well as much of the Win32 API. Because of its foundation,
104it is very familiar for programmers who have experience with MFC and the Win32
105API. It is obviously not compatible with other platforms and toolkits.
106PythonWin is organized as separate packages and modules so you can use the
107pieces you need without having to use the GUI portions.
108
109@subsection overview_python_othergui_others Others
110
111There are quite a few other GUI modules available for Python, some in active
112use, some that haven't been updated for ages. Most are simple wrappers around
113some C or C++ toolkit or another, and most are not cross-platform compatible.
3ed3a1c8
BP
114See <a href="http://pypi.python.org/pypi?:action=browse&show=all&c=433">this link</a>
115for a listing of a few of them.
2cd3cc94
BP
116
117
118@section overview_python_using Using wxPython
119
120I'm not going to try and teach the Python language here. You can do that at the
3ed3a1c8
BP
121<a href="http://www.python.org/doc/tut/tut.html">Python Tutorial</a>. I'm also
122going to assume that you know a bit about wxWidgets already, enough to notice
123the similarities in the classes used.
2cd3cc94
BP
124
125Take a look at the following wxPython program. You can find a similar program
3ed3a1c8 126in the @c wxPython/demo directory, named @c DialogUnits.py. If your Python and
2cd3cc94
BP
127wxPython are properly installed, you should be able to run it by issuing this
128command:
129
130@code
131python DialogUnits.py
132@endcode
133
134@code
13501: ## import all of the wxPython GUI package
13602: from wxPython.wx import *
13703:
13804: ## Create a new frame class, derived from the wxPython Frame.
13905: class MyFrame(wxFrame):
14006:
14107: def __init__(self, parent, id, title):
14208: # First, call the base class' __init__ method to create the frame
14309: wxFrame.__init__(self, parent, id, title,
14410: wxPoint(100, 100), wxSize(160, 100))
14511:
14612: # Associate some events with methods of this class
14713: EVT_SIZE(self, self.OnSize)
14814: EVT_MOVE(self, self.OnMove)
14915:
15016: # Add a panel and some controls to display the size and position
15117: panel = wxPanel(self, -1)
15218: wxStaticText(panel, -1, "Size:",
15319: wxDLG_PNT(panel, wxPoint(4, 4)), wxDefaultSize)
15420: wxStaticText(panel, -1, "Pos:",
15521: wxDLG_PNT(panel, wxPoint(4, 14)), wxDefaultSize)
15622: self.sizeCtrl = wxTextCtrl(panel, -1, "",
15723: wxDLG_PNT(panel, wxPoint(24, 4)),
15824: wxDLG_SZE(panel, wxSize(36, -1)),
15925: wxTE_READONLY)
16026: self.posCtrl = wxTextCtrl(panel, -1, "",
16127: wxDLG_PNT(panel, wxPoint(24, 14)),
16228: wxDLG_SZE(panel, wxSize(36, -1)),
16329: wxTE_READONLY)
16430:
16531:
16632: # This method is called automatically when the CLOSE event is
16733: # sent to this window
16834: def OnCloseWindow(self, event):
16935: # tell the window to kill itself
17036: self.Destroy()
17137:
17238: # This method is called by the system when the window is resized,
17339: # because of the association above.
17440: def OnSize(self, event):
17541: size = event.GetSize()
17642: self.sizeCtrl.SetValue("%s, %s" % (size.width, size.height))
17743:
17844: # tell the event system to continue looking for an event handler,
17945: # so the default handler will get called.
18046: event.Skip()
18147:
18248: # This method is called by the system when the window is moved,
18349: # because of the association above.
18450: def OnMove(self, event):
18551: pos = event.GetPosition()
18652: self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))
18753:
18854:
18955: # Every wxWidgets application must have a class derived from wxApp
19056: class MyApp(wxApp):
19157:
19258: # wxWidgets calls this method to initialize the application
19359: def OnInit(self):
19460:
19561: # Create an instance of our customized Frame class
19662: frame = MyFrame(NULL, -1, "This is a test")
19763: frame.Show(true)
19864:
2cd3cc94
BP
19967:
20068: # Return a success flag
20169: return true
20270:
20371:
20472: app = MyApp(0) # Create an instance of the application class
20573: app.MainLoop() # Tell it to start processing events
20674:
207@endcode
208
209@subsection overview_python_using_notice Things to Notice
210
211At line 2 the wxPython classes, constants, and etc. are imported into the
212current module's namespace. If you prefer to reduce namespace pollution you can
3ed3a1c8
BP
213use @c "from wxPython import wx" and then access all the wxPython identifiers
214through the wx module, for example, @c "wx.wxFrame".
2cd3cc94
BP
215
216At line 13 the frame's sizing and moving events are connected to methods of the
217class. These helper functions are intended to be like the event table macros
218that wxWidgets employs. But since static event tables are impossible with
219wxPython, we use helpers that are named the same to dynamically build the
220table. The only real difference is that the first argument to the event helpers
221is always the window that the event table entry should be added to.
222
223Notice the use of @c wxDLG_PNT and @c wxDLG_SZE in lines 19-29 to convert from
224dialog units to pixels. These helpers are unique to wxPython since Python can't
225do method overloading like C++.
226
3ed3a1c8 227There is an @c OnCloseWindow method at line 34 but no call to @c EVT_CLOSE to
2cd3cc94
BP
228attach the event to the method. Does it really get called? The answer is, yes
229it does. This is because many of the standard events are attached to windows
230that have the associated standard method names. I have tried to follow the lead
231of the C++ classes in this area to determine what is standard but since that
232changes from time to time I can make no guarantees, nor will it be fully
3ed3a1c8 233documented. When in doubt, use an @c EVT_*** function.
2cd3cc94
BP
234
235At lines 17 to 21 notice that there are no saved references to the panel or the
236static text items that are created. Those of you who know Python might be
237wondering what happens when Python deletes these objects when they go out of
238scope. Do they disappear from the GUI? They don't. Remember that in wxPython
239the Python objects are just shadows of the corresponding C++ objects. Once the
240C++ windows and controls are attached to their parents, the parents manage them
241and delete them when necessary. For this reason, most wxPython objects do not
3ed3a1c8 242need to have a @c __del__ method that explicitly causes the C++ object to be
2cd3cc94
BP
243deleted. If you ever have the need to forcibly delete a window, use the
244Destroy() method as shown on line 36.
245
246Just 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
18f42b94 248method should create the application's main window (line 62) and show it.
2cd3cc94
BP
249
250And finally, at line 72 an instance of the application class is created. At
251this point wxPython finishes initializing itself, and calls the @c OnInit
252method to get things started. (The zero parameter here is a flag for
253functionality that isn't quite implemented yet. Just ignore it for now.) The
254call to @c MainLoop at line 73 starts the event loop which continues until the
255application terminates or all the top level windows are closed.
256
257
258@section overview_python_classes Classes Implemented in wxPython
259
260The following classes are supported in wxPython. Most provide nearly full
261implementations of the public interfaces specified in the C++ documentation,
262others are less so. They will all be brought as close as possible to the C++
263spec 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
3ed3a1c8 291@li wxConfigBase
2cd3cc94
BP
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
3ed3a1c8 346@li @ref wxFileSystem "wxInternetFSHandler"
2cd3cc94
BP
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
3ed3a1c8 368@li wxMetafileDC
2cd3cc94
BP
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
f09b5681 406@li ::wxScrolledWindow
2cd3cc94
BP
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
3ed3a1c8 445@li @ref wxFileSystem "wxZipFSHandler"
2cd3cc94
BP
446
447
448@section overview_python_help Where to Go for Help
449
450Since wxPython is a blending of multiple technologies, help comes from multiple
451sources. See http://wxpython.org/ for details on various sources of help, but
452probably the best source is the wxPython-users mail list. You can view the
453archive or subscribe by going to http://wxpython.org/maillist.php
454
455Or you can send mail directly to the list using this address:
456wxpython-users@lists.wxwidgets.org
457
458*/