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