[wxPython-users] Binding Menu Events

Mike Eller meller1 at nc.rr.com
Sat Mar 15 18:08:41 PDT 2008


On Sat, 2008-03-15 at 18:00 -0700, Phil Mayes wrote:
> At 04:15 PM 3/15/2008, you wrote:
> >OK,
> >Update on my efforts...
> >
> >In the WorkoutMenu class I did this to generate the dynamic menu from a
> >file....this works as desired (creates a menu item under the view menu
> >for each item in the list)
> >
> >         for i in range(len(thelist)):
> >                 self.item = viewMenu.Append(-1, text=thelist[i])
> >
> >Next I did this in the MainWindow class which calls the WorkoutMenu
> >class to create the menu....
> >
> >         self.Bind(wx.EVT_MENU, self.OnViewUser(menuBar.item.Text,
> >menuBar.item.GetId()), id=menuBar.item.GetId())
> >
> >This also seems to do what I want in that it gets the text from the
> >menubar class and the id.....however, it calls the OnViewUser function
> >immediately upon running the application...not what I want...it does
> >nothing when I click on a View menu item.
> >The OnViewUser function looks like this...
> >
> >         def OnViewUser(self, text, myID):
> >                 user = text
> >                 print user, myID
> >                 if user == "Laurel":
> >                         print "Mike was selected."
> >
> >The prints are just for testing....
> >
> >So I feel I am getting closer....but not where I want to be.
> >Does this put any ideas into anyone's head?
> 
> 
> Your Bind call is executing the function, not passing its address.  Try 
> something like (untested):
>      self.Bind(wx.Evt_MENU, self.OnViewUser)
> 
>      def OnViewUser(self, event): # note only 1 param, an event object
>          item = event.GetEventObject()
>          text = self.m2f[item.GetId()]
>          print text
> 
> HTH, Phil


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'

> 
> 
> ---------------------------------------------------------------------
> 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