It looks like in the wxPython demo that they "code" the ids in a child parent fashion:
# top level menu
menu1 = wx.Menu()
menu1.Append(11,"11")
menu1.Append(12, "12")
# sub menu 1
menu2 = wx.Menu()
menu2.Append(131, "131")
menu2.Append(132, "132")
menu1.AppendMenu(13,"13",menu2)
# sub menu 2
menu3 = wx.Menu()
menu3.Append(1321,"1321")
menu3.Append(1322,"1322")
menu3.Append(1323,"1323")
menu2.AppendMenu(132, "132", menu3)
# add top to menubar
menubar.Append(menu1, "&Top")
I thought surely given the "clicked" menuitem you could walk the submenus backwards. This doesn't seem to be the case, as the clicked menuitem only holds a reference to it's parent menu and not the menuitem it is a submenu of.
So, the best I could code up was a nasty recursive function that walks top/down to find the submenu clicked.
def MenuClick(self, event):
def _menuItemSearch(menu,subMenuTree ,id):
if not menu.FindItemById(id): return False
# it is in this menu
for menuItem in menu.MenuItems:
if menuItem.GetId() == id:
subMenuTree.append(menuItem.GetLabel())
return True
if menuItem.GetSubMenu():
if _menuItemSearch(menuItem.GetSubMenu(),subMenuTree,id):
subMenuTree.append(menuItem.GetLabel())
return True
return False
subMenuTree = []
for menu,name in self.GetMenuBar().GetMenus():
_menuItemSearch(menu,subMenuTree,event.Id)
print subMenuTree
[u'1321', u'132', u'13']