In my application, which has a menu in the tray, I’d like to handle an event to show a submenu, and I’ve seen some strange behaviour. I can’t get the submenu object from the event.
I wrote a test application with a main menu and a context popup menu, and the event handling works differently in them. In the main menu, events in the submenu return the object that triggered the event, but in the popup menu they don’t.
import wx
class MenuFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
menubar = wx.MenuBar()
menubar.Append(build_menu(), "menu")
self.SetMenuBar(menubar)
self.Bind(wx.EVT_MENU_OPEN, on_menu)
self.Bind(wx.EVT_MENU_HIGHLIGHT, on_menu)
self.Bind(wx.EVT_CONTEXT_MENU, self.popup_menu)
def popup_menu(self, event):
self.PopupMenu(build_menu())
def on_menu(event):
print('--------')
print(event)
print(event.EventObject)
def build_menu():
menu = wx.Menu()
menuitem = menu.Append(wx.ID_ANY, 'MenuItem 1')
menuitem = menu.Append(wx.ID_ANY, 'MenuItem 2')
submenu = wx.Menu()
submenu.Append(wx.ID_ANY, 'SubMenuItem 1')
submenu.Append(wx.ID_ANY, 'SubMenuItem 2')
menu.AppendSubMenu(submenu, 'Submenu')
return menu
if __name__ == '__main__':
app = wx.App()
frame = MenuFrame()
frame.Show()
app.MainLoop()
For context submenu in console:
<wx._core.MenuEvent object at 0x0364A928>
None
Can anyone explain why this happens?