How can I get the id for a menu in the menubar?

I want to bind wx.EVT_MENU_OPEN, wx.MENU_CLOSE, and wx.MENU_HIGHLIGHT to wx.Menus that are direct children of a wx.MenuBar.

However, a series of roadblocks seems to be preventing me from doing this.

If I make a submenu of a wx.Menu, using wx.Menu.AppendMenu(), what gets appended is a wx.MenuItem that is a submenu. Since wx.MenuItem's have id's, I can bind the events using:

myAppObject.Bind(wx.EVT_MENU_HIGHLIGHT, myfunc, self)

where self is the wx.MenuItem instance.

But when you use the Append() function of the wx.MenuBar class, you append raw wx.Menus, which don't have id's of their own and so there doesn't appear to be a way to catch the events for those menus.

If I try to call wx.MenuBar.Append() by sending a wx.MenuItem instead of a wx.Menu, that fails.

I guess my question is: how do I bind to a wx.Menu's EVT_MENU_HIGHLIGHT when that wx.Menu is a child of the wx.MenuBar, and not a wx.Menu?

···

--
pkm ~ http://paulmcnett.com

Paul McNett wrote:

I guess my question is: how do I bind to a wx.Menu's EVT_MENU_HIGHLIGHT when that wx.Menu is a child of the wx.MenuBar, and not a wx.Menu?

Ok, I found part of the answer:

wxAppObject.Bind(wx.EVT_MENU_HIGHLIGHT, self.onHighlight, self.GetFrame())

where self is the menubar instance. However, since those top-level wx.Menus don't have id's of their own, how do I discriminate? I'm now receiving the events, but the ones for the top-level wx.Menus are coming with evt.GetId() == -1.

···

--
pkm ~ http://paulmcnett.com

Paul McNett wrote:

Paul McNett wrote:

I guess my question is: how do I bind to a wx.Menu's EVT_MENU_HIGHLIGHT when that wx.Menu is a child of the wx.MenuBar, and not a wx.Menu?

Ok, I found part of the answer:

wxAppObject.Bind(wx.EVT_MENU_HIGHLIGHT, self.onHighlight, self.GetFrame())

where self is the menubar instance. However, since those top-level wx.Menus don't have id's of their own, how do I discriminate? I'm now receiving the events, but the ones for the top-level wx.Menus are coming with evt.GetId() == -1.

I'm having a discussion with myself here, I hope it is amusing! I just found in the docs for MenuEvent the GetMenu() and GetMenuId() functions, which appear to lead me on the correct path.

···

--
pkm ~ http://paulmcnett.com