[wxPython-users] Binding Menu Events
Mike Eller
meller1 at nc.rr.com
Sun Mar 16 10:31:51 PDT 2008
On Sat, 2008-03-15 at 19:25 -0700, Phil Mayes wrote:
> At 06:08 PM 3/15/2008, you wrote:
> >OK,
> >
> >Now I get the following error....on all menu events (close, edit, etc)
> >Traceback (most recent call last):
> > File "MainWindow.py", line 39, in OnViewUser
> > text = self.m2f[item.GetId()]
> >AttributeError: 'MainFrame' object has no attribute 'm2f'
>
> Guess I was too brief...! See my original post, where self.m2f is a
> dictionary mapping the menu IDs to the labels on the menu. Basically, the
> idea is to save whatever info you want, referenced by the menu ID, then
> retrieve it in the menu handler.
>
> Phil
Phil,
I see it now. Had to read up on dictionaries.
So here is what I did...for completeness for this thread.
In the class that creates the menubar....I did this:
first, I created a constant wxID_VIEWUSER = 130
Then, after reading in the file which lists the items I want in my
"View" menu, I did this to dynamically add each item:
for i in range(len(thelist)): #thelist is the items from file
#I give each menu item a unique id by adding list index to the
constantID
viewMenu.Append((wxID_VIEWUSER + i), text=thelist[i])
#then I add the id and text to the dictionary
self.mylist[(wxID_VIEWUSER + i)] = thelist[i]
In my main class, I set the menu events for the "View" menu like this:
for i, j in self.menuBar.mylist.iteritems():
self.Bind(wx.EVT_MENU, self.OnViewUser, id=i)
Then the ViewMenu event handler looks like this: (print is just for
testing)
def OnViewUser(self, event):
user = self.menuBar.mylist[event.GetId()]
print user
Now each user's text is printed when the appropriate menu item is
clicked...just as I wanted.
Phil, Thank you for the guidance and patience!
If any of the code/techniques look bad or there is a better more
efficient way....please let me know. I want to do things right.
As I progress through this...I am sure I will be back for more!
Again Thanks
Mike
>
>
> ---------------------------------------------------------------------
> 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