[wxPython-users] How to know what event IDs to use in menu?

Phillip Stevens pmstevens at verizon.net
Mon Apr 23 10:00:18 PDT 2007


Hi Grant,

For the reasons you mention about creating possible conflicts with
ID's I have started using wx.NewID():

        ID = wx.NewId()
        menu1.Append(ID, '&Open', 'Open')
        self.Bind(wx.EVT_MENU, self.OnFileOpen, id=ID)

        menu1.AppendSeparator()           

        ID = wx.NewId()
        menu1.Append(ID, '&Save', 'Save')
        self.Bind(wx.EVT_MENU, self.OnFileSave, id=ID)

There are some constants wx.ID_LOWEST and wx.ID_HIGHEST that I read
about in "WxPython In Action" (pg 42-43) that you can also use to
prevent conflicts.



Monday, April 23, 2007, 12:13:16 PM, you wrote:

> I've been using wxPython for probably 5 years now, and I'm
> afraid I still don't "get" event IDs.

> For example, I don't understand how one knows what event IDs to
> use when doing things like binding handlers to menu items.
> Here's the example from the tutorial:

>     from wxPython.wx import *
>     
>     ID_ABOUT = 101
>     ID_EXIT  = 102
>     
>     class MyFrame(wxFrame):
>         def __init__(self, parent, ID, title):
>             wxFrame.__init__(self, parent, ID, title,
>                              wxDefaultPosition, wxSize(200, 150))
>             self.CreateStatusBar()
>             self.SetStatusText("This is the statusbar")
>     
>             menu = wxMenu()
>             menu.Append(ID_ABOUT, "&About",
>                         "More information about this program")
>             menu.AppendSeparator()
>             menu.Append(ID_EXIT, "E&xit", "Terminate the program")
>     
>             menuBar = wxMenuBar()
>             menuBar.Append(menu, "&File");
>     
>             self.SetMenuBar(menuBar)

>             EVT_MENU(self, ID_ABOUT, self.OnAbout)      
>             EVT_MENU(self, ID_EXIT,  self.TimeToQuit)


> Where do the values 101 and 102 come from?  How are they
> different from wx.ID_ABOUT and wx.ID_EXIT.  When would you use
> wx.ID_ABOUT or wx.ID_EXIT?  

> When pulling numbers like 101 and 102 out of the air, how do
> you know they don't conflict with event IDs wx is using for
> something else?  Is the event "number space" considered to be
> global?

> Why should one even have to pull a bunch of arbitrary numbers
> out of the air in order to connect a handler to a menu?  Why
> not just tell the menu what hanlder to call?

>   menu.Append("&About","More information about this program",handler=self.OnAbout)

> If oen were to subclass wx.Menu to do that, what are the rules
> for generating event IDs?
>   





More information about the wxpython-users mailing list