views:

30

answers:

1

Let's say in a larger submenu structure with a depth of 3 levels, I have selected 'car' in the first level, 'type' in the second, and 'suv' in the third and last level. Is there any way I can figure all these three selections in my def OnPopupItemSelected(self, event) method?

I hope I have made myself clear enough, if not, please add a comment so I can re-phrase.

A: 

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']
Mark
Yea I thought about that, but it's a tad longwinded. It would be nicer to have the id of the menuitem it is a submenu of. But like you say, it doesn't seem wxPython is structured that way.
c00kiemonster