How do I find a specific menu item in a menu

There are a number of ways in wxPython to find a specific menu or menu item given a label or an identifier. You often use these methods in event handlers, especially when you want to modify a menu item or display its label text in another location. Listing 10.4 augments the previous dynamic menu example by using FindltemByld() to get the appropriate menu item for display.

Listing 10.4 Finding a specific menu item import wx class MyFrame(wx.Frame):

"Find Item Example")

self.txt = wx.TextCtrl(p, -1, "new item") btn = wx.Button(p, -1, "Add Menu Item") self.Bind(wx.EVT_BUTTON, self.OnAddItem, btn)

sizer = wx.BoxSizer(wx.HORIZONTAL) sizer.Add(self.txt, 0, wx.ALL, 20) sizer.Add(btn, 0, wx.TOP|wx.RIGHT, 20) p.SetSizer(sizer)

simple = menu.Append(-1, "Simple menu item")


self.Bind(wx.EVT_MENU, self.OnSimple, simple)

self.Bind(wx.EVT_MENU, self.OnExit, exit)

menuBar = wx.MenuBar() menuBar.Append(menu, "Menu") self.SetMenuBar(menuBar)

def OnSimple(self, event):

wx.MessageBox("You selected the simple menu item")

def OnExit(self, event): self.Close()

def OnAddItem(self, event):

item =, self.txt.GetValue()) self.Bind(wx.EVT_MENU, self.OnNewltemSelected, item)

def OnNewItemSelected(self, event):

item = self.GetMenuBar().FindItemById(event.GetId()) text = item.GetText()

wx.MessageBox("You selected the '%s' item" % text)

if __name__ == "__main__": app = wx.PySimpleApp() frame = MyFrame() frame.Show() app.MainLoop()

In this example, FindltemByld() is used to get the item for the purpose of getting its text label for the display.

Both wx.MenuBar and wx.Menu have essentially the same methods for finding out information about specific menu items. The primary difference is that the wx.MenuBar methods will find an item anywhere on the menu bar, while the wx.Menu items will find an item only if it is in that particular menu. For most uses, the wx.MenuBar items are preferred, at least in part because the menu bar is easily accessible using the wx.Frame.GetMenuBar() method.

To find a top-level menu from the menu bar, use the menu bar method Find-Menu(title). This method returns either the index of the appropriate menu or the constant wx.NOT_FOUND. To get the actual menu, use GetMenu():

def FindMenuInMenuBar(menuBar, title): pos = menuBar.FindMenu(title) if pos == wx.NOT_FOUND:

return None return menuBar.GetMenu(pos)

The title parameter of FindMenu matches the menu title with or without the decorator label characters discussed next. For example, FindMenu("File") still matches the menu item even if its label was instantiated as &File. All of the methods in these menu classes that find a menu item based on a label string share this functionality.

Table 10.4 specifies the methods of wx.MenuBar which can be used to find or manipulate a specific menu item.

Getting the menu item

Table 10.4 Menu item manipulation methods of wx.MenuBar



FindMenuItem(menuString, itemString)

Searches for a menu named menuString and a menu item named itemString within that menu. Returns the found menu item or wx.NOT_FOUND.


Returns the menu item associated with the given wxPython identifier. If there is no such item, returns None.

GetHelpString(id) SetHelpString(id, helpString)

Getter and setter for the help string of the menu item with the given wxPython identifier. If there is no such menu item, then the getter returns "". If there is no such menu item, the setter has no effect.

GetLabel(id) SetLabel(id, label)

Getter and setter for the display label of the menu item with the given wxPython identifier. Manages nonexistent ID the same way as the help string methods. These methods should only be used after the menu bar has been associated with a frame.

Table 10.5 displays a similar menu item API for wx.Menu. The methods behave similarly to the menu bar counterparts, except that the returned menu item must be in the invoked menu instance.

After the menu item is returned, you may want to do something useful, such as enable or disable the item. In the next section, we'll discuss enabling and disabling menu items.

Table 10.5 Menu Item methods of wx.Menu




Returns the menu item associated with the given itemString, or wx.NOT_FOUND


As the menu bar method


Returns the menu item associated with the given position in the menu


SetHelpString(id, helpString)

As the menu bar methods

GetLabel(id) SetLabel(id, helpString)

As the menu bar methods

Was this article helpful?

+1 -1

Post a comment