C M,
<snip>
Mike, thinking about it further based on your example ("Basically, I'm
creating a list of tuples that contains a random id and a key that
corresponds to a menu item title. ") I thought, well if it is being
turned into a list of tuples anyway, maybe I should lose the dicts
altogether and try it with just tuples. I had been thinking before
that I somehow *should* use dicts, but I've tried it now with tuples
of tuples...Basically the idea is to have a data tuple like (title, ID,
bitmap)--here I am adding a bitmap, too--like this:self.popmenuData = ( ('Coke',wx.NewId(),self.coke_bmp) ,
('Pepsi',wx.NewId(),self.pepsi_bmp ),
('Fanta',wx.NewId(),self.fanta_bmp ),('Moxie',wx.NewId(),self.moxie_bmp ) )
# and then when I want to use it, loop through it and tuple indices, like:
for tuple in self.popmenuData:
title = tuple[0]; popID = tuple[1]; bitmap = tuple[2]
item = wx.MenuItem(menu, popID, title)
item.SetBitmap(bitmap)
menu.AppendItem(item)
self.Bind(wx.EVT_MENU, self.OnPopupMenu, id=popID)# and then the handler gets the title by looping through the tuple and
matching the ID:def OnPopupMenu(self,event):
for tuple in self.popmenuData:
if tuple[1] == event.GetId(): #if tuple's ID matches event's ID
title = tuple[0]
break
print titleThis may not be the "right" way to do it, but I find it easy to follow the code
and may either go with this or your example or something in between. I have
learned some things in this process due to your efforts, thanks.-cm
I thought about trying some thing other than dicts too...but since you and the venerable Demo used dicts, I felt compelled to try it that way. As with most languages, there are multiple paths to the same result. I'm glad you found something that's easier to grasp. Check out Tim's example too. It looks pretty interesting as well.
Mike