[wxPython-users] Binding Menu Events
Mike Eller
meller1 at nc.rr.com
Sat Mar 15 16:15:13 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,
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?
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