1 ============================
2 wxPython 2.5 Migration Guide
3 ============================
5 This document will help explain some of the major changes in wxPython
6 2.5 and let you know what you need to do to adapt your programs to
7 those changes. Be sure to also check in the CHANGES.txt file like
8 usual to see info about the not so major changes and other things that
9 have been added to wxPython.
15 The **wxWindows** project and library is now known as
16 **wxWidgets**. Please see here_ for more details.
18 .. _here: http://www.wxwidgets.org/name.htm
20 This won't really affect wxPython all that much, other than the fact
21 that the wxwindows.org domain name will be changing to wxwidgets.org,
22 so mail list, CVS, and etc. addresses will be changing. We're going
23 to try and smooth the transition as much as possible, but I wanted you
24 all to be aware of this change if you run into any issues.
31 The import-startup-bootstrap process employed by wxPython was changed
32 such that wxWidgets and the underlying gui toolkit are **not**
33 initialized until the wx.App object is created (but before wx.App.OnInit
34 is called.) This was required because of some changes that were made
35 to the C++ wxApp class.
37 There are both benefits and potential problems with this change. The
38 benefits are that you can import wxPython without requiring access to
39 a GUI (for checking version numbers, etc.) and that in a
40 multi-threaded environment the thread that creates the app object will
41 now be the GUI thread instead of the one that imports wxPython. Some
42 potential problems are that the C++ side of the "stock-objects"
43 (wx.BLUE_PEN, wx.TheColourDatabase, etc.) are not initialized until
44 the wx.App object is created, so you should not use them until after
45 you have created your wx.App object. If you do then an exception will
46 be raised telling you that the C++ object has not been initialized
49 Also, you will probably not be able to do any kind of GUI or bitmap
50 operation unless you first have created an app object, (even on
51 Windows where most anything was possible before.)
58 wxPython is now using SWIG 1.3.x from CVS (with several of my own
59 customizations added that I hope to get folded back into the main SWIG
60 distribution.) This has some far reaching ramifications:
62 All classes derive from object and so all are now "new-style
65 Public data members of the C++ classes are wrapped as Python
66 properties using property() instead of using __getattr__/__setattr__
67 like before. Normally you shouldn't notice any difference, but if
68 you were previously doing something with __getattr__/__setattr__
69 in derived classes then you may have to adjust things.
71 Static C++ methods are wrapped using the staticmethod()
72 feature of Python and so are accessible as ClassName.MethodName
73 as expected. They are still available as top level functions
74 ClassName_MethodName as before.
76 The relationship between the wxFoo and wxFooPtr classes have
77 changed for the better. Specifically, all instances that you see
78 will be wxFoo even if they are created internally using wxFooPtr,
79 because wxFooPtr.__init__ will change the instance's __class__ as
80 part of the initialization. If you have any code that checks
81 class type using something like isinstance(obj, wxFooPtr) you will
82 need to change it to isinstance(obj, wxFoo).
89 All of the EVT_* functions are now instances of the wx.PyEventBinder
90 class. They have a __call__ method so they can still be used as
91 functions like before, but making them instances adds some
92 flexibility that I expect to take advantave of in the future.
94 wx.EvtHandler (the base class for wx.Window) now has a Bind method that
95 makes binding events to windows a little easier. Here is its
96 definition and docstring::
98 def Bind(self, event, handler, source=None, id=wxID_ANY, id2=wxID_ANY):
100 Bind an event to an event handler.
102 event One of the EVT_* objects that specifies the
103 type of event to bind.
105 handler A callable object to be invoked when the event
106 is delivered to self. Pass None to disconnect an
109 source Sometimes the event originates from a different window
110 than self, but you still want to catch it in self. (For
111 example, a button event delivered to a frame.) By
112 passing the source of the event, the event handling
113 system is able to differentiate between the same event
114 type from different controls.
116 id,id2 Used for menu IDs or for event types that require a
121 Some examples of its use::
123 self.Bind(wx.EVT_SIZE, self.OnSize)
124 self.Bind(wx.EVT_BUTTON, self.OnButtonClick, theButton)
125 self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
128 The wx.Menu methods that add items to a wx.Menu have been modified
129 such that they return a reference to the wx.MenuItem that was created.
130 Additionally menu items and toolbar items have been modified to
131 automatically generate a new ID if -1 is given, similar to using -1
132 with window classess. This means that you can create menu or toolbar
133 items and event bindings without having to predefine a unique menu ID,
134 although you still can use IDs just like before if you want. For
135 example, these are all equivallent other than their specific ID
139 item = menu.Append(-1, "E&xit", "Terminate the App")
140 self.Bind(wx.EVT_MENU, self.OnExit, item)
143 item = menu.Append(wx.ID_EXIT, "E&xit", "Terminate the App")
144 self.Bind(wx.EVT_MENU, self.OnExit, item)
147 menu.Append(wx.ID_EXIT, "E&xit", "Terminate the App")
148 self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
151 If you create your own custom event types and EVT_* functions, and you
152 want to be able to use them with the Bind method above then you should
153 change your EVT_* to be an instance of wxPyEventBinder instead of a
154 function. For example, if you used to have something like this::
156 myCustomEventType = wxNewEventType()
157 def EVT_MY_CUSTOM_EVENT(win, id, func):
158 win.Connect(id, -1, myCustomEventType, func)
163 myCustomEventType = wx.NewEventType()
164 EVT_MY_CUSTOM_EVENT = wx.PyEventBinder(myCustomEventType, 1)
166 The second parameter is an integer in [0, 1, 2] that specifies the
167 number of IDs that are needed to be passed to Connect.
176 The second phase of the wx Namespace Transition has begun. That means
177 that the real names of the classes and other symbols do not have the
178 'wx' prefix and the modules are located in a Python package named
179 wx. There is still a Python package named wxPython with modules
180 that have the names with the wx prefix for backwards compatibility.
181 Instead of dynamically changing the names at module load time like in
182 2.4, the compatibility modules are generated at build time and contain
183 assignment statements like this::
185 wxWindow = wx.core.Window
187 Don't let the "core" in the name bother you. That and some other
188 modules are implementation details, and everything that was in the
189 wxPython.wx module before will still be in the wx package namespace
190 after this change. So from your code you would use it as wx.Window.
192 A few notes about how all of this was accomplished might be
193 interesting... SWIG is now run twice for each module that it is
194 generating code for. The first time it outputs an XML representaion
195 of the parse tree, which can be up to 20MB and 300K lines in size!
196 That XML is then run through a little Python script that creates a
197 file full of SWIG %rename directives that take the wx off of the
198 names, and also generates the Python compatibility file described
199 above that puts the wx back on the names. SWIG is then run a second
200 time to generate the C++ code to implement the extension module, and
201 uses the %rename directives that were generated in the first step.
203 Not every name is handled correctly (but the bulk of them are) and so
204 some work has to be done by hand, especially for the reverse-renamers.
205 So expect a few flaws here and there until everything gets sorted out.
207 In summary, the wx package and names without the "wx" prefix are now
208 the official form of the wxPython classes. For example::
212 class MyFrame(wx.Frame):
213 def __init__(self, parent, title):
214 wx.Frame.__init__(self, parent, -1, title)
215 p = wx.Panel(self, -1)
216 b = wx.Button(p, -1, "Do It", (10,10))
217 self.Bind(wx.EVT_BUTTON, self.JustDoIt, b)
219 def JustDoIt(self, evt):
222 app = wx.PySimpleApp()
223 f = MyFrame(None, "What's up?")
227 You shouldn't need to migrate all your modules over to use the new
228 package and names right away as there are modules in place that try to
229 provide as much backwards compatibility of the names as possible. If
230 you rewrote the above sample using "from wxPython.wx import * ", the
231 old wxNames, and the old style of event binding it will still work
240 Many of the Draw methods of wx.DC have alternate forms in C++ that take
241 wxPoint or wxSize parameters (let's call these *Type A*) instead of
242 the individual x, y, width, height, etc. parameters (and we'll call
243 these *Type B*). In the rest of the library I normally made the *Type
244 A* forms of the methods be the default method with the "normal" name,
245 and had renamed the *Type B* forms of the methods to some similar
246 name. For example in wx.Window we have these Python methods::
248 SetSize(size) # Type A
249 SetSizeWH(width, height) # Type B
252 For various reasons the new *Type A* methods in wx.DC were never added
253 and the existing *Type B* methods were never renamed. Now that lots
254 of other things are also changing in wxPython it has been decided that
255 it is a good time to also do the method renaming in wx.DC too in order
256 to be consistent with the rest of the library. The methods in wx.DC
257 that are affected are listed here::
259 FloodFillXY(x, y, colour, style = wx.FLOOD_SURFACE)
260 FloodFill(point, colour, style = wx.FLOOD_SURFACE)
265 DrawLineXY(x1, y1, x2, y2)
266 DrawLine(point1, point2)
271 DrawArcXY(x1, y1, x2, y2, xc, yc)
272 DrawArc(point1, point2, center)
274 DrawCheckMarkXY(x, y, width, height)
277 DrawEllipticArcXY(x, y, w, h, start_angle, end_angle)
278 DrawEllipticArc(point, size, start_angle, end_angle)
283 DrawRectangleXY(x, y, width, height)
284 DrawRectangle(point, size)
285 DrawRectangleRect(rect)
287 DrawRoundedRectangleXY(x, y, width, height, radius)
288 DrawRoundedRectangle(point, size, radius)
289 DrawRoundedRectangleRect(rect, radius)
291 DrawCircleXY(x, y, radius)
292 DrawCircle(point, radius)
294 DrawEllipseXY(x, y, width, height)
295 DrawEllipse(point, size)
296 DrawEllipseRect(rect)
298 DrawIconXY(icon, x, y)
299 DrawIcon(icon, point)
301 DrawBitmapXY(bmp, x, y, useMask = FALSE)
302 DrawBitmap(bmp, point, useMask = FALSE)
304 DrawTextXY(text, x, y)
305 DrawText(text, point)
307 DrawRotatedTextXY(text, x, y, angle)
308 DrawRotatedText(text, point, angle)
311 BlitXY(xdest, ydest, width, height, sourceDC, xsrc, ysrc,
312 rop = wxCOPY, useMask = FALSE, xsrcMask = -1, ysrcMask = -1)
313 Blit(destPt, size, sourceDC, srcPt,
314 rop = wxCOPY, useMask = FALSE, srcPtMask = wx.DefaultPosition)
316 SetClippingRegionXY(x, y, width, height)
317 SetClippingRegion(point, size)
318 SetClippingRect(rect)
319 SetClippingRegionAsRegion(region);
322 If you have code that draws on a DC and you are using the new wx
323 namespace then you **will** get errors because of these changes, but
324 it should be easy to fix the code. You can either change the name of
325 the *Type B* method called to the names shown above, or just add
326 parentheses around the parameters as needed to turn them into tuples
327 and let the SWIG typemaps turn them into the wx.Point or wx.Size
328 object that is expected. Then you will be calling the new *Type A*
329 method. For example, if you had this code before::
331 dc.DrawRectangle(x, y, width, height)
333 You could either continue to use the *Type B* method bu changing the
334 name to DrawRectabgleXY, or just change it to the new *Type A* by
335 adding some parentheses like this::
337 dc.DrawRectangle((x, y), (width, height))
339 Or if you were already using a point and size::
341 dc.DrawRectangle(p.x, p.y, s.width, s.height)
343 Then you can just simplify it like this::
345 dc.DrawRectangle(p, s)
347 Now before you start yelling and screaming at me for breaking all your
348 code, take note that I said above "...using the new wx namespace..."
349 That's because if you are still importing from wxPython.wx then there
350 are some classes defined there with Draw and etc. methods that have
351 2.4 compatible signatures. However if/when the old wxPython.wx
352 namespace is removed then these classes will be removed too so you
353 should plan on migrating to the new namespace and new DC Draw methods
358 Building, Extending and Embedding wxPython
359 ------------------------------------------
361 wxPython's setup.py script now expects to use existing libraries for
362 the contribs (gizmos, stc, xrc, etc.) rather than building local
363 copies of them. If you build your own copies of wxPython please be
364 aware that you now need to also build the ogl, stc, xrc, and gizmos
365 libraries in addition to the main wx lib.
367 The wxPython.h and other header files are now in
368 .../wxPython/include/wx/wxPython instead of in wxPython/src. You should
369 include it via the "wx/wxPython/wxPython.h" path and add
370 .../wxPython/include to your list of include paths. On OSX and
371 unix-like systems the wxPython headers are installed to the same place
372 that the wxWidgets headers are installed, so if you building wxPython
373 compatible extensions on those platforms then your include path shoudl
374 already be set properly.
376 If you are also using SWIG for your extension then you'll need to
377 adapt how the wxPython .i files are imported into your .i files. See
378 the wxPython sources for examples. Your modules will need to at least
379 ``%import core.i``, and possibly others if you need the definition of
380 other classes. Since you will need them to build your modules, the
381 main wxPython .i files are also installed with the wxPython headers in
382 an i_files sibdirectory. It should be enough to pass a -I/pathname on
383 the command line for it to find the files.
385 The bulk of wxPython's setup.py has been moved to another module,
386 wx/build/config.py. This module will be installed as part of wxPython
387 so 3rd party modules that wish to use the same setup/configuration
388 code can do so simply by importing this module from their own setup.py
389 scripts using ``import wx.build.config``.
391 You no longer need to call wxClassInfo::CleanUpClasses() and
392 wxClassInfo::InitializeClasses() in your extensions or when embedding
395 The usage of wxPyBeginAllowThreads and wxPyEndAllowThreads has changed
396 slightly. wxPyBeginAllowThreads now returns a boolean value that must
397 be passed to the coresponding wxPyEndAllowThreads function call. This
398 is to help do the RightThing when calls to these two functions are
399 nested, or if calls to external code in other extension modules that
400 are wrapped in the standard Py_(BEGIN|END)_ALLOW_THERADS may result in
401 wx event handlers being called (such as during the call to
406 Two (or Three!) Phase Create
407 ----------------------------
409 If you use the Precreate/Create method of instantiating a window, (for
410 example, to set an extended style flag, or for XRC handlers) then
411 there is now a new method named PostCreate to help with transplanting
412 the brain of the prewindow instance into the derived window instance.
415 class MyDialog(wx.Dialog):
416 def __init__(self, parent, ID, title, pos, size, style):
418 pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
419 pre.Create(parent, ID, title, pos, size, style)
427 The hack allowing the old "option" keyword parameter has been removed.
428 If you use keyworkd args with w.xSizer Add, Insert, or Prepend methods
429 then you will need to use the ``proportion`` name instead of ``option``.
431 When adding a spacer to a sizer you now need to use a wx.Size or a
432 2-integer sequence instead of separate width and height parameters.
434 The wx.GridBagSizer class (very similar to the RowColSizer in the
435 library) has been added to C++ and wrapped for wxPython. It can also
438 You should not use AddWindow, AddSizer, AddSpacer (and similar for
439 Insert, Prepend, and etc.) methods any longer. Just use Add and the
440 wrappers will figure out what to do.
446 Added wx.PlatformInfo which is a tuple containing strings that
447 describe the platform and build options of wxPython. This lets you
448 know more about the build than just the __WXPORT__ value that
449 wx.Platform contains, such as if it is a GTK2 build. For example,
452 if wx.Platform == "__WXGTK__":
457 if "__WXGTK__" in wx.PlatformInfo:
460 and you can specifically check for a wxGTK2 build by looking for
461 "gtk2" in wx.PlatformInfo. Unicode builds are also detectable this
462 way. If there are any other platform/toolkit/build flags that make
463 sense to add to this tuple please let me know.
465 BTW, wx.Platform will probably be deprecated in the future.
472 Lindsay Mathieson's newest wxActiveX_ class has been wrapped into a new
473 extension module called wx.activex. It is very generic and dynamic
474 and should allow hosting of arbitray ActiveX controls within your
475 wxPython apps. So far I've tested it with IE, PDF, and Flash
476 controls, (and there are new samples in the demo and also library
477 modules supporting these.)
479 .. _wxActiveX: http://members.optusnet.com.au/~blackpaw1/wxactivex.html
481 The new wx.activex module contains a bunch of code, but the most
482 important things to look at are ActiveXWindow and ActiveXEvent.
483 ActiveXWindow derives from wxWindow and the constructor accepts a
484 CLSID for the ActiveX Control that should be created. (There is also
485 a CLSID class that can convert from a progID or a CLSID String.) The
486 ActiveXWindow class simply adds methods that allow you to query some
487 of the TypeInfo exposed by the ActiveX object, and also to get/set
488 properties or call methods by name. The Python implementation
489 automatically handles converting parameters and return values to/from
490 the types expected by the ActiveX code as specified by the TypeInfo,
491 (just bool, integers, floating point, strings and None/Empty so far,
492 but more can be handled later.)
494 That's pretty much all there is to the class, as I mentioned before it
495 is very generic and dynamic. Very little is hard-coded and everything
496 that is done with the actual ActiveX control is done at runtime and
497 referenced by property or method name. Since Python is such a dynamic
498 language this is a very good match. I thought for a while about doing
499 some Python black-magic and making the specific methods/properties of
500 the actual ActiveX control "appear" at runtime, but then decided that
501 it would be better and more understandable to do it via subclassing.
502 So there is a utility class in wx.activex that given an existing
503 ActiveXWindow instance can generate a .py module containing a derived
504 class with real methods and properties that do the Right Thing to
505 reflect those calls to the real ActiveX control. There is also a
506 script/tool module named genaxmodule that given a CLSID or progID and
507 a class name, will generate the module for you. There are a few
508 examples of the output of this tool in the wx.lib package, see
509 iewin.py, pdfwin.py and flashwin.py.
511 Currently the genaxmodule tool will tweak some of the names it
512 generates, but this can be controled if you would like to do it
513 differently by deriving your own class from GernerateAXModule,
514 overriding some methods and then using this class from a tool like
515 genaxmodule. [TODO: make specifying a new class on genaxmodule's
516 command-line possible.] The current default behavior is that any
517 event names that start with "On" will have the "On" dropped, property
518 names are converted to all lower case, and if any name is a Python
519 keyword it will have an underscore appended to it. GernerateAXModule
520 does it's best when generating the code in the new module, but it can
521 only be as good as the TypeInfo data available from the ActiveX
522 control so sometimes some tweaking will be needed. For example, the
523 IE web browser control defines the Flags parameter of the Navigate2
524 method as required, but MSDN says it is optional.
526 It is intended that this new wx.activex module will replace both the
527 older version of Lindsay's code available in iewin.IEHtmlWindow, and
528 also the wx.lib.activexwraper module. Probably the biggest
529 differences you'll ecounter in migrating activexwrapper-based code
530 (besides events working better without causing deadlocks) is that
531 events are no longer caught by overriding methods in your derived
532 class. Instead ActiveXWindow uses the wx event system and you bind
533 handlers for the ActiveX events exactly the same way you do for any wx
534 event. There is just one extra step needed and that is creating an
535 event ID from the ActiveX event name, and if you use the genaxmodule
536 tool then this extra step will be handled for you there. For example,
537 for the StatusTextChange event in the IE web browser control, this
538 code is generated for you::
540 wxEVT_StatusTextChange = wx.activex.RegisterActiveXEvent('StatusTextChange')
541 EVT_StatusTextChange = wx.PyEventBinder(wxEVT_StatusTextChange, 1)
543 and you would use it in your code like this::
545 self.Bind(iewin.EVT_StatusTextChange, self.UpdateStatusText, self.ie)
547 When the event happens and your event handler function is called the
548 event properties from the ActiveX control (if any) are converted to
549 attributes of the event object passed to the handler. (Can you say
550 'event' any more times in a single sentence? ;-) ) For example the
551 StatusTextChange event will also send the text that should be put into
552 the status line as an event parameter named "Text" and you can access
553 it your handlers as an attribute of the event object like this::
555 def UpdateStatusText(self, evt):
556 self.SetStatusText(evt.Text)
558 Usually these event object attributes should be considered read-only,
559 but some will be defined by the TypeInfo as output parameters. In
560 those cases if you modify the event object's attribute then that value
561 will be returned to the ActiveX control. For example, to prevent a
562 new window from being opened by the IE web browser control you can do
563 this in the handler for the iewin.EVT_NewWindow2 event::
565 def OnNewWindow2(self, evt):
568 So how do you know what methods, events and properties that an ActiveX
569 control supports? There is a funciton in wx.activex named GetAXInfo
570 that returns a printable summary of the TypeInfo from the ActiveX
571 instance passed in. You can use this as an example of how to browse
572 the TypeInfo provided, and there is also a copy of this function's
573 output appended as a comment to the modules produced by the
574 genaxmodule tool. Beyond that you'll need to consult the docs
575 provided by the makers of the ActiveX control that you are using.
582 Instead of over a dozen separate extension modules linked together
583 into a single extension module, the "core" module is now just a few
584 extensions that are linked independently, and then merged together
585 later into the main namespace via Python code.
587 Because of the above and also because of the way the new SWIG works,
588 the "internal" module names have changed, but you shouldn't have been
589 using them anyway so it shouldn't bother you. ;-)
591 The help module no longer exists and the classes therein are now part
592 of the core module imported with wxPython.wx or the wx package.
594 wxPyDefaultPosition and wxPyDefaultSize are gone. Use the
595 wxDefaultPosition and wxDefaultSize objects instead.
597 Similarly, the wxSystemSettings backwards compatibiility aliases for
598 GetSystemColour, GetSystemFont and GetSystemMetric have also gone into
599 the bit-bucket. Use GetColour, GetFont and GetMetric instead.
602 The wx.NO_FULL_REPAINT_ON_RESIZE style is now the default style for
603 all windows. The name still exists for compatibility, but it is set
604 to zero. If you want to disable the setting (so it matches the old
605 default) then you need to use the new wx.FULL_REPAINT_ON_RESIZE style
606 flag otherwise only the freshly exposed areas of the window will be
609 wxPyTypeCast has been removed. Since we've had the OOR (Original
610 Object Return) for a couple years now there should be no need to use
613 If you use the old wxPython package and wxPython.wx namespace then
614 there are compatibility aliases for much of the above items.
616 The wxWave class has been renamed to wxSound, and now has a slightly
619 wx.TaskbarIcon works on wxGTK-based platforms now, however you have to
620 manage it a little bit more than you did before. Basically, the app
621 will treat it like a top-level frame in that if the wx.TaskBarIcon
622 still exists when all the frames are closed then the app will still
623 not exit. You need to ensure that the wx.TaskBarIcon is destroyed
624 when your last Frame is closed. For wxPython apps it is usually
625 enough if your main frame object holds the only reference to the
626 wx.TaskBarIcon, then when the frame is closed Python reference
627 counting takes care of the rest.