[wxPython-users] Binding mouse clicks to toolbar button
Robin Dunn
robin at alldunn.com
Tue Sep 25 10:00:33 PDT 2007
Cody Precord wrote:
> Think your looking for wx.EVT_TOOL
EVT_TOOL is, for all intents and purposes, the same as EVT_MENU. So
this enables a little trick that may not be readily apparent from Python
(but is used often in C++) which is that if the menu items and their
corresponding toolbar items have the same ID, then you can handle both
with the same event binding. They can also be enabled/disabled with the
same EVT_UPDATE_UI binding too.
To make use of this trick you can create one of the items first (either
the menu item or the toolbar item,) and then use item.GetId() for the ID
when you create the other. This will help simplify the code somewhat,
but may not suit everybody's sense of style. ;-)
item = menu.Append(wx.NewId(), "Blah", "blah, blah, blah")
toolbar.AddSimpleTool(item.GetId(), bitmap, "Blah", "blah, blah, blah")
self.Bind(wx.EVT_MENU, self.OnDoBlah, item)
I'm sure you can see how this could easily be put into it's own method
to simplify things even further...
>> wx.Image('resources/help-browser.png',
>> wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
BTW, no need to create a wx.Image if you are just going to convert it to
a bitmap. The wx.Bitmap constructor will do all of this itself if it
needs to. This will work just as well, and is a lot less to type:
wx.Bitmap('resources/help-browser.png')
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list