[wxPython-users] Binding Menu Events
Phil Mayes
phil at philmayes.com
Fri Mar 14 15:28:32 PDT 2008
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
More information about the wxpython-users
mailing list