python.h 15 KB

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