Search code examples
pythonuser-interfacewxpythonwxwidgets

wxPython - How to get MenuItem from event?


Using wxPython, I have a frame setup with a menu and a status bar. The menu is generated from an indented text file, from which I use to create a nice nested menu bar with each menu item bound to a specific function. I have a "Toggle Status Bar" menu check item which is bound OnToggleStatusBar().

I would like to see if the menu item is checked or not and react accordingly, but I cannot seem to access the menuItem from the event. If I use GetId(), how can that be used to find the menu item? I tried event.GetId() with FindWindowById() but got nothing. I also tried event.GetEventObject(), which returned a menu but not a menu item.

def OnToggleStatusBar(self, event):
    id = event.GetId()
    menu = event.GetEventObject()
    menuItem = menu.FindWindowById(id) #does not work
    print self.FindByWindowId(id) # prints None

Solution

  • You don't need to find the item, you can use wxMenuBar::IsChecked(), which will do it for you, directly. And you can either just store the menu bar in self.menuBar or retrieve it from the frame using its GetMenuBar() method.