Question

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?

Was it helpful?

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()

OTHER TIPS

There are lots of ways to do this, but a standard and generic approach is to use functools.partial:

f = functools.partial(self.MenuClicked, my_id_1)
self.Bind(wx.EVT_MENU, f, id=self.menu1)

where my_id_1 is some identifier, possibly self.menu1 if you like, and then elsewhere:

def MenuClicked(self, my_id, evt):
    print my_id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top