已經提供了一個解決你的問題,我意識到應該有另一種實現我的解決方案的方式。事實上,我可以自己採用這種方式,因爲它更清潔,記憶力更少。這使用event.GetEventObject()
方法。
import wx
class MenuCallback(wx.Frame):
def __init__(self, *args, **kwds):
self.frame=wx.Frame.__init__(self, *args, **kwds)
self.menubar = wx.MenuBar()
# self.statusbar = wx.StatusBar(self-1)
self.CreateStatusBar()
self.SetStatusText("Demonstration of wxPython")
menu1 = wx.Menu()
menu_item_1 = menu1.Append(wx.ID_OPEN, "&File")
menu_item_2 = menu1.Append(wx.ID_EXIT, "&Exit...")
#Build a list of things via another function or just a declaration
self.list_of_things = ["Thing No 1", "Thing No 2", "Thing No 3"]
list_used = wx.Menu()
thing_count = 101
for thing in self.list_of_things:
t1 = wx.MenuItem(list_used, thing_count, thing)
list_used.AppendItem(t1)
thing_count +=1
thing_end = wx.MenuItem(list_used,199,'End of List')
list_used.AppendItem(thing_end)
menu1.AppendMenu(wx.ID_FILE,'&Things',list_used)
menu1.SetHelpString(wx.ID_FILE, 'Select from the list of things')
self.menubar.Append(menu1, "&File")
self.SetMenuBar(self.menubar)
# Create bindings for the Thing list
i_count = 101
for i in self.list_of_things:
self.Bind(wx.EVT_MENU, self.OnThingOpen, id=i_count)
i_count = i_count + 1
self.Bind(wx.EVT_MENU, self.OnThingEnd, id=199)
self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_EXIT)
self.Show(True)
def OnThingOpen(self, event):
id_selected = event.GetId()
obj = event.GetEventObject()
print "Option =", id_selected
print obj.GetLabel(id_selected)
print "From the range:"
for i in range(obj.MenuItemCount):
print "\t\t", obj.MenuItems[i].GetLabel()
def OnThingEnd(self, event):
pass
def OnClose(self, event):
self.Close()
if __name__ == '__main__':
app = wx.App()
MC=MenuCallback(parent=None, id=-1)
app.MainLoop()
只是一個'wx.PySimpleApp'已被棄用的FYI。我不認爲它會在wxPython Phoenix中可用。 –