Working with wxPython IDs

Table 2.1 lists the wxPython ID number of the new frame. ID numbers are a feature of all widgets in wxPython, and it's worth a few paragraphs to explain how they work. Every widget in a wxPython application has a window identifier. The ID numbers must be unique within each frame, but you can reuse ids between frames. We recommend, however, that you try to have your ID numbers be unique across your application, to prevent errors and confusion when processing events. However, there are few standard predefined ID numbers in wxPython, which have specific meanings within parts of the code (for example, wx.ID_OK and wx.ID_ cancel are assumed to be the ID numbers of the OK and Cancel buttons in a dialog box). It's usually not a problem to reuse standard ID numbers in your application as long as you use them in the manner expected. The ID number is usually the second parameter in the constructor of a wxPython widget object, after the parent widget. The most important use of ID numbers in wxPython is to create a unique relationship between an event that happens to a specific object and a function which is called in response to that event. Using a duplicate ID can cause the wrong function to be triggered in response to an event.

There are three ways to create the ID numbers used by a widget:

1 Explicitly pass a positive integer into the constructor

2 Get wxPython to create IDs for you using the wx.NewId() function

3 Pass either the global constant wx.id_any or -1 to a widget constructor

Was this article helpful?

0 0

Post a comment