]>
Commit | Line | Data |
---|---|---|
1 | ============================ | |
2 | wxPython 2.5 Migration Guide | |
3 | ============================ | |
4 | ||
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. | |
10 | ||
11 | ||
12 | ||
13 | Module Initialization | |
14 | --------------------- | |
15 | ||
16 | The import-startup-bootstrap process employed by wxPython was changed | |
17 | such that wxWindows and the underlying gui toolkit are **not** | |
18 | initialized until the wx.App object is created (but before wx.App.OnInit | |
19 | is called.) This was required because of some changes that were made | |
20 | to the C++ wxApp class. | |
21 | ||
22 | There are both benefits and potential problems with this change. The | |
23 | benefits are that you can import wxPython without requiring access to | |
24 | a GUI (for checking version numbers, etc.) and that in a | |
25 | multi-threaded environment the thread that creates the app object will | |
26 | now be the GUI thread instead of the one that imports wxPython. Some | |
27 | potential problems are that the C++ side of the "stock-objects" | |
28 | (wx.BLUE_PEN, wx.TheColourDatabase, etc.) are not initialized until | |
29 | the wx.App object is created, so you should not use them until after | |
30 | you have created your wx.App object. If you do then an exception will | |
31 | be raised telling you that the C++ object has not bene initialized | |
32 | yet. | |
33 | ||
34 | Also, you will probably not be able to do any kind of GUI or bitmap | |
35 | operation unless you first have created an app object, (even on | |
36 | Windows where most anything was possible before.) | |
37 | ||
38 | ||
39 | ||
40 | SWIG 1.3 | |
41 | -------- | |
42 | ||
43 | wxPython is now using SWIG 1.3.x from CVS (with several of my own | |
44 | customizations added that I hope to get folded back into the main SWIG | |
45 | distribution.) This has some far reaching ramifications: | |
46 | ||
47 | All classes derive from object and so all are now "new-style | |
48 | classes" | |
49 | ||
50 | Public data members of the C++ classes are wrapped as Python | |
51 | properties using property() instead of using __getattr__/__setattr__ | |
52 | like before. Normally you shouldn't notice any difference, but if | |
53 | you were previously doing something with __getattr__/__setattr__ | |
54 | in derived classes then you may have to adjust things. | |
55 | ||
56 | Static C++ methods are wrapped using the staticmethod() | |
57 | feature of Python and so are accessible as ClassName.MethodName | |
58 | as expected. They are still available as top level functions | |
59 | ClassName_MethodName as before. | |
60 | ||
61 | The relationship between the wxFoo and wxFooPtr classes have | |
62 | changed for the better. Specifically, all instances that you see | |
63 | will be wxFoo even if they are created internally using wxFooPtr, | |
64 | because wxFooPtr.__init__ will change the instance's __class__ as | |
65 | part of the initialization. If you have any code that checks | |
66 | class type using something like isinstance(obj, wxFooPtr) you will | |
67 | need to change it to isinstance(obj, wxFoo). | |
68 | ||
69 | ||
70 | ||
71 | Binding Events | |
72 | -------------- | |
73 | ||
74 | All of the EVT_* functions are now instances of the wx.PyEventBinder | |
75 | class. They have a __call__ method so they can still be used as | |
76 | functions like before, but making them instances adds some | |
77 | flexibility. | |
78 | ||
79 | wx.EvtHandler (the base class for wx.Window) now has a Bind method that | |
80 | makes binding events to windows a little easier. Here is its | |
81 | definition and docstring:: | |
82 | ||
83 | def Bind(self, event, handler, source=None, id=wxID_ANY, id2=wxID_ANY): | |
84 | """ | |
85 | Bind an event to an event handler. | |
86 | ||
87 | event One of the EVT_* objects that specifies the | |
88 | type of event to bind. | |
89 | ||
90 | handler A callable object to be invoked when the event | |
91 | is delivered to self. Pass None to disconnect an | |
92 | event handler. | |
93 | ||
94 | source Sometimes the event originates from a different window | |
95 | than self, but you still want to catch it in self. (For | |
96 | example, a button event delivered to a frame.) By | |
97 | passing the source of the event, the event handling | |
98 | system is able to differentiate between the same event | |
99 | type from different controls. | |
100 | ||
101 | id,id2 Used for menu IDs or for event types that require a | |
102 | range of IDs | |
103 | ||
104 | """ | |
105 | ||
106 | Some examples of its use:: | |
107 | ||
108 | self.Bind(wx.EVT_SIZE, self.OnSize) | |
109 | self.Bind(wx.EVT_BUTTON, self.OnButtonClick, theButton) | |
110 | self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT) | |
111 | ||
112 | ||
113 | The wx.Menu methods that add items to a wx.Menu have been modified | |
114 | such that they return a reference to the wx.MenuItem that was created. | |
115 | Additionally menu items and toolbar items have been modified to | |
116 | automatically generate a new ID if -1 is given, similar to using -1 | |
117 | with window classess. This means that you can create menu or toolbar | |
118 | items and event bindings without having to predefine a unique menu ID, | |
119 | although you still can use IDs just like before if you want. For | |
120 | example, these are all equivallent other than ID values:: | |
121 | ||
122 | 1. | |
123 | item = menu.Append(-1, "E&xit", "Terminate the App") | |
124 | self.Bind(wx.EVT_MENU, self.OnExit, item) | |
125 | ||
126 | 2. | |
127 | item = menu.Append(wx.ID_EXIT, "E&xit", "Terminate the App") | |
128 | self.Bind(wx.EVT_MENU, self.OnExit, item) | |
129 | ||
130 | 3. | |
131 | menu.Append(wx.ID_EXIT, "E&xit", "Terminate the App") | |
132 | self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT) | |
133 | ||
134 | ||
135 | If you create your own custom event types and EVT_* functions, and you | |
136 | want to be able to use them with the Bind method above then you should | |
137 | change your EVT_* to be an instance of wxPyEventBinder instead of a | |
138 | function. If you used to have something like this:: | |
139 | ||
140 | myCustomEventType = wxNewEventType() | |
141 | def EVT_MY_CUSTOM_EVENT(win, id, func): | |
142 | win.Connect(id, -1, myCustomEventType, func) | |
143 | ||
144 | ||
145 | Change it like so:: | |
146 | ||
147 | myCustomEventType = wx.NewEventType() | |
148 | EVT_MY_CUSTOM_EVENT = wx.PyEventBinder(myCustomEventType, 1) | |
149 | ||
150 | The second parameter is an integer in [0, 1, 2] that specifies the | |
151 | number of IDs that are needed to be passed to Connect. | |
152 | ||
153 | ||
154 | ||
155 | ||
156 | ||
157 | The wx Namespace | |
158 | ---------------- | |
159 | ||
160 | The second phase of the wx Namespace Transition has begun. That means | |
161 | that the real names of the classes and other symbols do not have the | |
162 | 'wx' prefix and the modules are located in a Python package named | |
163 | wx. There is still a Python package named wxPython with modules | |
164 | that have the names with the wx prefix for backwards compatibility. | |
165 | Instead of dynamically changing the names at module load time like in | |
166 | 2.4, the compatibility modules are generated at build time and contain | |
167 | assignment statements like this:: | |
168 | ||
169 | wxWindow = wx.core.Window | |
170 | ||
171 | Don't let the "core" in the name bother you. That and some other | |
172 | modules are implementation details, and everything that was in the | |
173 | wxPython.wx module before will still be in the wx package namespace | |
174 | after this change. So from your code you would use it as wx.Window. | |
175 | ||
176 | A few notes about how all of this was accomplished might be | |
177 | interesting... SWIG is now run twice for each module that it is | |
178 | generating code for. The first time it outputs an XML representaion | |
179 | of the parse tree, which can be up to 20MB and 300K lines in size! | |
180 | That XML is then run through a little Python script that creates a | |
181 | file full of SWIG %rename directives that take the wx off of the | |
182 | names, and also generates the Python compatibility file described | |
183 | above that puts the wx back on the names. SWIG is then run a second | |
184 | time to generate the C++ code to implement the extension module, and | |
185 | uses the %rename directives that were generated in the first step. | |
186 | ||
187 | Not every name is handled correctly (but the bulk of them are) and so | |
188 | some work has to be done by hand, especially for the reverse-renamers. | |
189 | So expect a few flaws here and there until everything gets sorted out. | |
190 | ||
191 | In summary, the wx package and names without the "wx" prefix are now | |
192 | the official form of the wxPython classes. For example:: | |
193 | ||
194 | import wx | |
195 | ||
196 | class MyFrame(wx.Frame): | |
197 | def __init__(self, parent, title): | |
198 | wx.Frame.__init__(self, parent, -1, title) | |
199 | p = wx.Panel(self, -1) | |
200 | b = wx.Button(p, -1, "Do It", (10,10)) | |
201 | self.Bind(wx.EVT_BUTTON, self.JustDoIt, b) | |
202 | ||
203 | def JustDoIt(self, evt): | |
204 | print "It's done!" | |
205 | ||
206 | app = wx.PySimpleApp() | |
207 | f = MyFrame(None, "What's up?") | |
208 | f.Show() | |
209 | app.MainLoop() | |
210 | ||
211 | You shouldn't need to migrate all your modules over to use the new | |
212 | package and names right away as there are modules in place that try to | |
213 | provide as much backwards compatibility of the names as possible. If | |
214 | you rewrote the above sample using "from wxPython.wx import * ", the | |
215 | old wxNames, and the old style of event binding it will still work | |
216 | just fine. | |
217 | ||
218 | ||
219 | ||
220 | ||
221 | New wx.DC Methods | |
222 | ----------------- | |
223 | ||
224 | Many of the Draw methods of wx.DC have alternate forms in C++ that take | |
225 | wxPoint or wxSize parameters (let's call these *Type A*) instead of | |
226 | the individual x, y, width, height, etc. parameters (and we'll call | |
227 | these *Type B*). In the rest of the library I normally made the *Type | |
228 | A* forms of the methods be the default method with the "normal" name, | |
229 | and had renamed the *Type B* forms of the methods to some similar | |
230 | name. For example in wx.Window we have these Python methods:: | |
231 | ||
232 | SetSize(size) # Type A | |
233 | SetSizeWH(width, height) # Type B | |
234 | ||
235 | ||
236 | For various reasons the new *Type A* methods in wx.DC were never added | |
237 | and the existing *Type B* methods were never renamed. Now that lots | |
238 | of other things are also changing in wxPython it has been decided that | |
239 | it is a good time to also do the method renaming in wx.DC too in order | |
240 | to be consistent with the rest of the library. The methods in wx.DC | |
241 | that are affected are listed here:: | |
242 | ||
243 | FloodFillXY(x, y, colour, style = wx.FLOOD_SURFACE) | |
244 | FloodFill(point, colour, style = wx.FLOOD_SURFACE) | |
245 | ||
246 | GetPixelXY(x, y) | |
247 | GetPixel(point) | |
248 | ||
249 | DrawLineXY(x1, y1, x2, y2) | |
250 | DrawLine(point1, point2) | |
251 | ||
252 | CrossHairXY(x, y) | |
253 | CrossHair(point) | |
254 | ||
255 | DrawArcXY(x1, y1, x2, y2, xc, yc) | |
256 | DrawArc(point1, point2, center) | |
257 | ||
258 | DrawCheckMarkXY(x, y, width, height) | |
259 | DrawCheckMark(rect) | |
260 | ||
261 | DrawEllipticArcXY(x, y, w, h, start_angle, end_angle) | |
262 | DrawEllipticArc(point, size, start_angle, end_angle) | |
263 | ||
264 | DrawPointXY(x, y) | |
265 | DrawPoint(point) | |
266 | ||
267 | DrawRectangleXY(x, y, width, height) | |
268 | DrawRectangle(point, size) | |
269 | DrawRectangleRect(rect) | |
270 | ||
271 | DrawRoundedRectangleXY(x, y, width, height, radius) | |
272 | DrawRoundedRectangle(point, size, radius) | |
273 | DrawRoundedRectangleRect(rect, radius) | |
274 | ||
275 | DrawCircleXY(x, y, radius) | |
276 | DrawCircle(point, radius) | |
277 | ||
278 | DrawEllipseXY(x, y, width, height) | |
279 | DrawEllipse(point, size) | |
280 | DrawEllipseRect(rect) | |
281 | ||
282 | DrawIconXY(icon, x, y) | |
283 | DrawIcon(icon, point) | |
284 | ||
285 | DrawBitmapXY(bmp, x, y, useMask = FALSE) | |
286 | DrawBitmap(bmp, point, useMask = FALSE) | |
287 | ||
288 | DrawTextXY(text, x, y) | |
289 | DrawText(text, point) | |
290 | ||
291 | DrawRotatedTextXY(text, x, y, angle) | |
292 | DrawRotatedText(text, point, angle) | |
293 | ||
294 | ||
295 | BlitXY(xdest, ydest, width, height, sourceDC, xsrc, ysrc, | |
296 | rop = wxCOPY, useMask = FALSE, xsrcMask = -1, ysrcMask = -1) | |
297 | Blit(destPt, size, sourceDC, srcPt, | |
298 | rop = wxCOPY, useMask = FALSE, srcPtMask = wx.DefaultPosition) | |
299 | ||
300 | SetClippingRegionXY(x, y, width, height) | |
301 | SetClippingRegion(point, size) | |
302 | SetClippingRect(rect) | |
303 | SetClippingRegionAsRegion(region); | |
304 | ||
305 | ||
306 | If you have code that draws on a DC and you are using the new wx | |
307 | namespace then you **will** get errors because of these changes, but | |
308 | it should be easy to fix the code. You can either change the name of | |
309 | the *Type B* method called to the names shown above, or just add | |
310 | parentheses around the parameters as needed to turn them into tuples | |
311 | and let the SWIG typemaps turn them into the wx.Point or wx.Size | |
312 | object that is expected. Then you will be calling the new *Type A* | |
313 | method. For example, if you had this code before:: | |
314 | ||
315 | dc.DrawRectangle(x, y, width, height) | |
316 | ||
317 | You could either continue to use the *Type B* method bu changing the | |
318 | name to DrawRectabgleXY, or just change it to the new *Type A* by | |
319 | adding some parentheses like this:: | |
320 | ||
321 | dc.DrawRectangle((x, y), (width, height)) | |
322 | ||
323 | Or if you were already using a point and size:: | |
324 | ||
325 | dc.DrawRectangle(p.x, p.y, s.width, s.height) | |
326 | ||
327 | Then you can just simplify it like this:: | |
328 | ||
329 | dc.DrawRectangle(p, s) | |
330 | ||
331 | Now before you start yelling and screaming at me for breaking all your | |
332 | code, take note that I said above "...using the new wx namespace..." | |
333 | That's because if you are still importing from wxPython.wx then there | |
334 | are some classes defined there with Draw and etc. methods that have | |
335 | 2.4 compatible signatures. However if/when the old wxPython.wx | |
336 | namespace is removed then these classes will be removed too so you | |
337 | should plan on migrating to the new namespace and new DC Draw methods | |
338 | before that time. | |
339 | ||
340 | ||
341 | ||
342 | Building, Extending and Embedding wxPython | |
343 | ------------------------------------------ | |
344 | ||
345 | wxPython's setup.py script now expects to use existing libraries for | |
346 | the contribs (gizmos, stc, xrc, etc.) rather than building local | |
347 | copies of them. If you build your own copies of wxPython please be | |
348 | aware that you now need to also build the ogl, stc, xrc, and gizmos | |
349 | libraries in addition to the main wx lib. [[TODO: update the | |
350 | BUILD.*.txt files too!]] | |
351 | ||
352 | The wxPython.h and other header files are now in | |
353 | .../wxPython/include/wx/wxPython instead of in wxPython/src. You should | |
354 | include it via the "wx/wxPython/wxPython.h" path and add | |
355 | .../wxPython/include to your list of include paths. [[TODO: Install | |
356 | these headers on Linux...]] | |
357 | ||
358 | You no longer need to call wxClassInfo::CleanUpClasses() and | |
359 | wxClassInfo::InitializeClasses() in your extensions or when embedding | |
360 | wxPython. | |
361 | ||
362 | ||
363 | ||
364 | ||
365 | Two (or Three!) Phase Create | |
366 | ---------------------------- | |
367 | ||
368 | If you use the Precreate/Create method of instantiating a window, (for | |
369 | example, to set an extended style flag, or for XRC handlers) then | |
370 | there is now a new method named PostCreate to help with transplanting | |
371 | the brain of the prewindow instance into the derived window instance. | |
372 | For example:: | |
373 | ||
374 | class MyDialog(wx.Dialog): | |
375 | def __init__(self, parent, ID, title, pos, size, style): | |
376 | pre = wx.PreDialog() | |
377 | pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP) | |
378 | pre.Create(parent, ID, title, pos, size, style) | |
379 | self.PostCreate(pre) | |
380 | ||
381 | ||
382 | ||
383 | Sizers | |
384 | ------ | |
385 | ||
386 | The hack allowing the old "option" keyword parameter has been removed. | |
387 | If you use keyworkd args with wxSizer Add, Insert, or Prepend methods | |
388 | then you will need to use the "proportion" name instead of "option". | |
389 | ||
390 | When adding a spacer to a sizer you now need to use a wxSize or a | |
391 | 2-integer sequence instead of separate width and height parameters. | |
392 | ||
393 | The wxGridBagSizer class (very similar to the RowColSizer in the | |
394 | library) has been added to C++ and wrapped for wxPython. It can also | |
395 | be used from XRC. | |
396 | ||
397 | You should not use AddWindow, AddSizer, AddSpacer (and similar for | |
398 | Insert, Prepend, and etc.) methods any longer. Just use Add and the | |
399 | wrappers will figure out what to do. | |
400 | ||
401 | ||
402 | ||
403 | Other Stuff | |
404 | ----------- | |
405 | ||
406 | Instead of over a dozen separate extension modules linked together | |
407 | into a single extension module, the "core" module is now just a few | |
408 | extensions that are linked independently, and then merged together | |
409 | later into the main namespace via Python code. | |
410 | ||
411 | Because of the above and also because of the way the new SWIG works, | |
412 | the "internal" module names have changed, but you shouldn't have been | |
413 | using them anyway so it shouldn't bother you. ;-) | |
414 | ||
415 | The help module no longer exists and the classes therein are now part | |
416 | of the core module imported with wxPython.wx or the wx package. | |
417 | ||
418 | wxPyDefaultPosition and wxPyDefaultSize are gone. Use the | |
419 | wxDefaultPosition and wxDefaultSize objects instead. | |
420 | ||
421 | Similarly, the wxSystemSettings backwards compatibiility aliases for | |
422 | GetSystemColour, GetSystemFont and GetSystemMetric have also gone into | |
423 | the bit-bucket. Use GetColour, GetFont and GetMetric instead. | |
424 | ||
425 | ||
426 | The wx.NO_FULL_REPAINT_ON_RESIZE style is now the default style for | |
427 | all windows. The name still exists for compatibility, but it is set | |
428 | to zero. If you want to disable the setting (so it matches the old | |
429 | default) then you need to use the new wx.FULL_REPAINT_ON_RESIZE style | |
430 | flag otherwise only the freshly exposed areas of the window will be | |
431 | refreshed. | |
432 | ||
433 | wxPyTypeCast has been removed. Since we've had the OOR (Original | |
434 | Object Return) for a couple years now there should be no need to use | |
435 | wxPyTypeCast at all. | |
436 | ||
437 | If you use the old wxPython package and wxPython.wx namespace then | |
438 | there are compatibility aliases for much of the above items. | |
439 | ||
440 | The wxWave class has been renamed to wxSound, and now has a slightly | |
441 | different API. |