I have a set of menus which I am developing in a project. Here is the basic code:
#Define main menu bar with sub-menus
OverMenuBar = wx.MenuBar()
#Define FileMenu
FileMenu = wx.Menu()
NewItem = FileMenu.Append(-1, “New”, “New item”)
OpenItem = FileMenu.Append(-1, “Open”, “Open file”)
self.Bind(wx.EVT_MENU, self.OnNew, NewItem)
self.Bind(wx.EVT_MENU, self.OnOpen, OpenItem)
OverMenuBar.Append(FileMenu, “File”)
self.SetMenuBar(OverMenuBar)
def OnNew(self, event):
wx.MessageBox(“New item”)
def OnOpen(self, event):
wx.MessageBox(“Open item”)
I would like to do this all with lists, because it would eliminate a bunch of duplicate code.
I am beginning with:
omitems = [(((“New”, “New item”, OnNew),
(“Open”, “Open file”, OnOpen)),“File”)]
for oitem in omitems:
oMenu = wx.Menu()
Link = []
iMenu = oMenu[0]
#print FileMenu
for item in iMenu:
Link = oMenu.Append(-1, item[0], item[1])
print item
FileLink.append(Link)
self.Bind(wx.EVT_MENU, self.item[2], Link)
OverMenuBar.Append(oMenu, oitem[1])
I get an error:
NameError: global name ‘OnNew’ is not defined
when the list omitems is being read.
I am a little confused with how to put functions into list. How do I define OnNew as a function so that I can use this abstract approach?
it is an object method… so you access it from inside other class methods by calling self.methodName(arg1). If you want to call methodName on that object, but doing so outside the class methods, then you just do myObject.methodName(arg1).