Shish
Shish

Reputation: 2990

Setting icons in a wx.Menu separately to the item IDs

I have a menu:

Projects
  |-Project A
  |   |- Open
  |   |- Run
  |   \- Close
  \-Project B
      |- Open
      |- Run
      \- Close

I would like both of the "Open" menu items to have the wx.ID_OPEN icon -- but it looks like giving multiple menu entries the same ID confuses the event system. Is there a way that I can set the ID to be unique, but also set the icon to be the same?

(Question tagged as wxpython because that's what I'm using, but I imagine that this probably isn't language-specific?)

Upvotes: 0

Views: 935

Answers (2)

Mike Driscoll
Mike Driscoll

Reputation: 33081

I would just find an open icon and set each menu item to use that bitmap via its SetBitmap() method. Something like this should work:

img = wx.Image(img_filepath, wx.BITMAP_TYPE_ANY)
myMenuItem.SetBitmap(wx.BitmapFromImage(img))

Just make sure you get an image that is the correct size (like 10x10).

Upvotes: 0

Jack burridge
Jack burridge

Reputation: 520

Use the same ID but bind the event differently, this should fix the confusion.

pa_open = wx.MenuItem(pa_menu, wx.ID_OPEN, "Open", "", wx.ITEM_NORMAL)
pb_open = wx.MenuItem(pb_menu, wx.ID_OPEN, "Open", "", wx.ITEM_NORMAL)

pa_open.Bind(wx.EVT_MENU, self.onOpenProjectA)
pb_open.Bind(wx.EVT_MENU, self.onOpenProjectB)

Or if its just the same icons you want you could set each menu item to wx.ID_ANY and use ArtProvider to set your icons. For example:

pa_open.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_OPEN))
pb_open.SetBitmap(wx.ArtProvider.GetBitmap(wx.ART_OPEN))

Upvotes: 1

Related Questions