[wxPython-users] Binding Menu Events
Mike Eller
meller1 at nc.rr.com
Sat Mar 15 08:05:09 PDT 2008
On Fri, 2008-03-14 at 15:28 -0700, Phil Mayes wrote:
> At 10:38 AM 3/14/2008, you wrote:
> >Hello List,
> >
> >I am learning Python and wxWidgets.
> >
> >In doing this, I have myself a little project.
> >
> >Overview: I just recently purchased a home workout gym (Bowflex
> >Revolution). Myself, my wife, and my oldest son are using it.
> >
> >I decided to create a workout routine application to track our progress.
> >
> >So, right now I am at a sticking point.
> >I have the app creating dynamic menu items to enable the selection of
> >which user's workout routine to view. This works fine. The code below
> >is how I am doing this...all comments/criticisms/ideas welcome.
> >
> >Right now the project consists of two files, MainWindow.py and
> >WorkoutMenu.py.
> >
> >The MainWindow is the base file that runs the app. WorkoutMenu creates
> >the menubar.
> >
> >In WorkoutMenu, this part dynamically creates the "View" menu.
> >
> > #The View Menu
> > viewMenu = wx.Menu()
> >
> > #Loop here to dynamically add users - from a file
> > thelist = self.vm()
> > for i in range(len(thelist)):
> > item = viewMenu.Append(wxID_VIEWUSER, text=thelist[i])
> > self.Append(viewMenu, "&View")
> >
> >The function vm() which is defined in the WorkoutMenu class, opens a
> >file, reads in the list of users, and returns the list. Like I said,
> >this all works fine.
> >
> >What I want to do now is pass to the OnViewUser function, which is
> >defined in the MainWindow class, the name (or some value) of the menu
> >item selected. The OnViewUser function in the MainWindow class will be
> >the event handler for the view menu. I need to be able to pass it some
> >value to distinguish what dynamically created menu item was selected.
> >
> >Does this make sense to anyone?
> >
> >So, the above code reads a file and reads in the names Mike, Matt,
> >Laurel into a list. It creates the menu items under the View menu with
> >those names. I want to pass a value to the menu event handler in the
> >MainWindow class based on which name I click on. Right now, in the
> >MainWindow class, the menu events look like this...
> >
> > #Set menu events
> > self.Bind(wx.EVT_MENU, self.OnQuit, id=wxID_EXIT)
> > self.Bind(wx.EVT_MENU, self.OnEditUser, id=wxID_EDITUSER)
> > self.Bind(wx.EVT_MENU, self.OnViewUser, id=wxID_VIEWUSER)
> >
> >The IDs are defined in the WorkoutMenu file as such:
> >wxID_EXIT = wx.NewId()
> >wxID_EDITUSER = wx.NewId()
> >wxID_VIEWUSER = wx.NewId()
> >
> >and the related functions are (OnViewUser is not yet defined):
> >
> > def OnQuit(self, event=None):
> > """Exit application."""
> > self.Close()
> >
> > def OnEditUser(self, event=None):
> > """Open the User Editor."""
> > dlg = wx.Dialog(frame, title="User Editor", name="Editor")
> > dlg.ShowModal()
> > dlg.Destroy()
> >
> >Is it possible to make the selection distinction and pass it to the
> >other class where the functions are defined?
> >
> >Thank you,
> >Mike
>
> Try giving each menu item a unique ID and associating it to the name in a
> dictionary:
> item = menu.Append(-1, text)
> self.Bind(wx.EVT_MENU, self.OnViewUser, item)
> self.m2f[item.GetId()] = text
>
> Then in your handler:
> user = self.m2f[event.GetId()]
> ...
>
> HTH, Phil
>
OK,
I did this...
In the class that creates the menubar, I used the following as you
posted
for i in range(len(thelist)):
item = viewMenu.Append(-1, text=thelist[i])
Then in the Main class that creates the frame and calls the class to
create the menu I did this as you posted:
self.Bind(wx.EVT_MENU, self.OnViewUser, item)
self.m2f[item.GetId()] = text
and in the handler I did this:
user = self.m2f[event.GetId()]
if user == Mike:
print "Mike was selected."
When I try to run it, I get an error:
Traceback (most recent call last):
File "MainWindow.py", line 43, in <module>
frame = MainFrame(None, title="Workout Routines")
File "MainWindow.py", line 22, in __init__
self.Bind(wx.EVT_MENU, self.OnViewUser, item)
NameError: global name 'item' is not defined
Any ideas? Thoughts?
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