I am working through the wxPython In Action book and in Chapter 6 a scribble-like application is built that includes a menu of 4 color choices and a toolbar that has the same 4 color choices. Both the menu and the toolbar implement these as “Radio button like” items so that when one is selected the others cannot be.
What I noticed is that the two are not linked - so that selecting “Red” in the toolbar does not cause “Red” to be selected in the Menu and vice versa. Is there a recommended way of linking these so that when the toolbar button is pressed the menu item will toggle?
Hi,
I am working through the wxPython In Action book and in Chapter 6 a scribble-like application is built that includes a menu of 4 color choices and a toolbar that has the same 4 color choices. Both the menu and the toolbar implement these as "Radio button like" items so that when one is selected the others cannot be.
What I noticed is that the two are not linked - so that selecting "Red" in the toolbar does not cause "Red" to be selected in the Menu and vice versa. Is there a recommended way of linking these so that when the toolbar button is pressed the menu item will toggle?
Take a look at EVT_UPDATE_UI. You can see some good examples of it's use in the PyShell code. Basically you bind the event to the menu/toolbar items the same as you do for EVT_MENU or EVT_TOOL (and if you make them use the same IDs then you only have to bind the event once.) When you get the update event you just need to check the state in your app of whatever this particular menu/tool item is for, and then call a method of the event object that tells what you want to do with it.
In your example you've got menu and tool items that are going to be toggled between a checked and unchecked state. So let's say that in your menu/tool event handlers you do something like this to toggle a flag:
self.fooFlag = not self.fooFlag
Then your EVT_UPDATE_UI handler can just do this:
event.Check(self.fooFlag)
and wx will take care of the rest, updating the checked state for the item that the event was sent for.
···
--
Robin Dunn
Software Craftsman http://wxPython.org Java give you jitters? Relax with wxPython!