Search code examples
pythonuser-interfacewxpythonpopupmenu

Bind multiple wx.EVT_MENU to a same method?


There're about 5-6 menu items in the right click popup, and binding them to separate methods seems clumsy since there's a good chunk of codes can be reused, is it possible to do things like this?

self.Bind(wx.EVT_MENU, self.MenuClicked, id=self.menu1)
self.Bind(wx.EVT_MENU, self.MenuClicked, id=self.menu2)
self.Bind(wx.EVT_MENU, self.MenuClicked, id=self.menu3)
self.Bind(wx.EVT_MENU, self.MenuClicked, id=self.menu4)
self.Bind(wx.EVT_MENU, self.MenuClicked, id=self.menu5)

def MenuClicked(self, event):
    detect which menu being clicked
    assign specific values to several variables regarding the menu being clicked

    rest of the codes.

I noticed there's no GetMenu() available for wx.EVT_MENU, so basically how do you recognize which menu is being clicked?


Solution

  • I prefere binding them to seperate methods but each to there own :) You can use the GetId() method on the event and then compare it with your menu items.

    def MenuClicked(self, event):
        id_selected = event.GetId()