How to identify a menu from a wx.CommandEvent?

Hi Robin,

Thanks a lot for this solution! I think this will do just fine :wink:

- Jorgen

路路路

On 4/23/07, Robin Dunn <robin@alldunn.com> wrote:

Jorgen Bodde wrote:
> Hi all,
>
> I want to hook up multiple menu's to a single event, and from there
> determine the menu that is selected. So far I have this;
>
> smenu = wx.Menu()
> m =
> m.append(wx.MenuItem(smenu, wx.NewId(), "&Started", "",
> wx.ITEM_NORMAL))
> m[0].Data = songs.SS_STARTED

> I get the same data for every sub menu item I pressed, so CliendData
> does nothing for menus, the event object is always the top menu, and
> Int is always 0.
>
> Is there a way to have one handler and intelligently handle several
> menus in wxPython?

The Python MenuItem proxy objects are not tracked like widget objects
are, (they don't derive from the right class in C++ so I can't track
custom user data with them.) Instead a new proxy object is created on
the fly by SWIG whenever it is needed, and any extra attributes you
attached to the original proxies are lost.

What I would do instead in this case it to simply keep a dictionary that
maps the menu IDs to the data values you want. Something like this:

        self.menuMap = {}
        mi = wx.MenuItem(smenu, wx.NewId(), "&Started", "", wx.ITEM_NORMAL)
        self.menuMap[mi.GetId()] = songs.SS_STARTED

And later in the event handler you can get to the data value like this:

        data = self.menuMap[event.GetId()]

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org