[wxPython-users] Can't get menu title for popup menus in Windows
Chris Mellon
arkanes at gmail.com
Fri Feb 2 09:29:05 PST 2007
On 2/2/07, Isaac Wagner <geekyisaac at gmail.com> wrote:
> Unfortunately, my menus are generated from info collected at runtime,
> so I am never sure what the menu title will end up being. That makes
> it hard to create different handlers for the different menus.
>
You already have different handlers for different menus - that is
exactly what you're doing in your sample code. You're just doing
secondary dispatch - you have one menu event handler and you are
inspecting the menu title to determine where to go next. You can bind
the IDs directly and skip the seconday dispatch. Here's a sample:
import wx
class TFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
menu = wx.Menu("File")
menu.Append(wx.NewId(), "Item")
#submenu one
sm = wx.Menu("Submenu1")
id1, id2 = wx.NewId(), wx.NewId()
sm.Append(id1, "Subitem 1")
sm.Append(id2, "Subitem 2")
self.Bind(wx.EVT_MENU, self.OnSubmenuOne, id=id1)
self.Bind(wx.EVT_MENU, self.OnSubmenuOne, id=id2)
menu.AppendMenu(wx.NewId(), "Submenu One", sm)
sm = wx.Menu("Submenu2")
id1, id2 = wx.NewId(), wx.NewId()
sm.Append(id1, "Subitem 1")
sm.Append(id2, "Subitem 2")
self.Bind(wx.EVT_MENU, self.OnSubmenuTwo, id=id1)
self.Bind(wx.EVT_MENU, self.OnSubmenuTwo, id=id2)
menu.AppendMenu(wx.NewId(), "Submenu Two", sm)
mb = wx.MenuBar()
mb.Append(menu, "File")
self.SetMenuBar(mb)
def OnSubmenuOne(self, evt):
print "Do something one"
def OnSubmenuTwo(self, evt):
print "Do something two"
if __name__ == '__main__':
app = wx.App(False)
f = TFrame(None)
f.Show()
app.MainLoop()
More information about the wxpython-users
mailing list