Don't understand wxPython ids

Roger Binns rogerb at rogerbinns.com
Wed Apr 28 03:33:17 EDT 2004


Greg Krohn wrote:
> I can only think of one reasn why they chose to use ids in the first
> place.

Don't forget how old wxWidgets is, and that it has to interoperate
with the underlying toolkits.

> Assigning many objects the same id allows you to use one EVT_*
> call for those objects. But is that really a useful feature? Who knows?

Here is how I use the same id in multiple locations.  I can have a
menu entry, a toolbar button, and a button inside some HTML all
invoke the same function.  The only coupling between them is
the id number.

You can also use id ranges in wxPython 2.5 which makes it easy to
send a whole bunch of different items to the same handler.

  class FooFrame(wx.Frame):
      ID_FILE_DELETE=wx.NewId()

      def _init_(...):
          ...
          menu.Append(self.ID_FILE_DELETE, "&Delete", "Delete the file")
          ....
          toolbar.AddLabelTool(self.ID_FILE_DELETE, "Delete", ....)
          ....
          wx.EVT_MENU(self, self.ID_FILE_DELETE, self.OnFileDelete)
          ....
          wx.EVT_BUTTON(self, self.ID_FILE_DELETE, self.OnFileDelete)
          ....

       def OnStateChange(...):
          ....
          self.GetToolBar().EnableTool(self.ID_FILE_DELETE, True)
          ....

In my HTML:

   <wxp class="Button" module="wx">
   <param name="label"   value="Delete ..."/>
   <param name="id"      value="gui.FooFrame.ID_FILE_DELETE"/>
   </wxp>

Roger





More information about the Python-list mailing list