[wxPython-users] Can't get menu title for popup menus in Windows
Isaac Wagner
geekyisaac at gmail.com
Fri Feb 2 12:15:38 PST 2007
Sorry if I am being unclear. I've been programming all day and my
brain is having a hard time speaking human. :)
Normally, I would do the menus as you suggest. However, this
situation does not lend itself well to that solution. Here's
basically the menu structure I have:
SomeText->
Option 1
Option 2
Option 3
ItemA->
Option 1
Option 2
Option 3
SomethingElse->
Option 1
Option 2
Option 3
Information collected from runtime determines what the particular
submenu is called, in this case SomeText, ItemA, and SomethingElse.
However, the items on the submenus are always the same, in this case
Option 1, Option 2, Option 3.
Because the Submenus are generated from runtime info I have no idea
what they are ahead of time. This is why I am doing a secondary
dispatch. I need to determine which submenu the particular item
belongs to.
Thanks for the suggestions.
-Isaac
On 2/2/07, Chris Mellon <arkanes at gmail.com> wrote:
> 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()
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe at lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help at lists.wxwidgets.org
>
>
More information about the wxpython-users
mailing list