views:

109

answers:

1

I'm trying to create a wx.Menu that will be shared between a popup (called on right-click), and a sub menu accessible from the frame menubar. The following code demonstrates the problem.

If you open the "MENU>submenu" from the menubar the item "asdf" is visible. If you right click on the frame content area, "asdf" will be visible from there as well... however, returning to the menubar, you will find that "MENU>submenu" is vacant. Why is this happening and how can I fix it?

import wx
app = wx.PySimpleApp()
m = wx.Menu()
m.Append(-1, 'asdf')

def show_popup(evt):
   ''' R-click callback '''
   f.PopupMenu(m, (evt.X, evt.Y))

f = wx.Frame(None)
f.SetMenuBar(wx.MenuBar())
frame_menu = wx.Menu()
f.MenuBar.Append(frame_menu, 'MENU')
frame_menu.AppendMenu(-1,'submenu', m)
f.Show()
f.Bind(wx.EVT_RIGHT_DOWN, show_popup)
app.MainLoop()

Interestingly, appending the menu to MenuBar works, but is not the behavior I want:

import wx
app = wx.PySimpleApp()
m = wx.Menu()
m.Append(-1, 'asdf')

def show_popup(evt):
   f.PopupMenu(m, (evt.X, evt.Y))

f = wx.Frame(None)
f.SetMenuBar(wx.MenuBar())
f.MenuBar.Append(m, 'MENU')
f.Show()
f.Bind(wx.EVT_RIGHT_DOWN, show_popup)
app.MainLoop()
+1  A: 

I would make a function, create_menu, that creates and returns a wx.Menu object. Call it once to add it to your menu bar and call it in show_popup. So you're using separate Menu objects. Don't worry about creating them on each right-click, it's not a big deal.

FogleBird
Actually, it's much more convenient for them to be the same object for me because the menu will contain several check items that need to maintain the same state regardless of which way the menu is shown. I could synchronize them manually, but it'd be pretty obnoxious.
Adam Fraser
+1 that is the way it should be done, @Adam Fraser, you do not have to manually update anything, make single function which creates the menu and hooks update event and other events, and that is it.
Anurag Uniyal