question about creating an EVT_MENU in code

Howdy again,

I have a floating toolbar that I now know how to create thanks to this list, and Chris Barker in particular.

I have bound a menu item to show or hide the toolbar.
On the toolbar I have a close box, that I want to include so the user doesn't have to go to the menu to close the toolbar.

Now the tricky part: the menu item is wx.ITEM_CHECK style, so it's stateful.
Closing the toolbar is absolutely equivalent to clicking the menu item, including toggling the
state of the menu item.

There is more than one way to do this, but to help me learn, I want to implement this by creating and
processing the EVT_MENU in code. I was hoping this would take care of the state of the menu item, if
the code version were *identical* to manually clicking the item. This isn't the case as I am implementing it.

Following wxPIA, I have implemented to following code:

menuId = app.mainFrame.GetMenuBar().FindMenuItem('View','Show Toolbar')
newEvent = wx.CommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED, menuId)
app.mainFrame.GetEventHandler().ProcessEvent(newEvent)

this code does in fact toggle the toolbar, but it *doesn't* toggle the menu item. I can do this in code easily
enough or including Publish and Subscribe ala MVC... The question is, is there a way to trigger the event exactly as a mouse click would, including changing the check state.

Also, I assume wx.wxEVT_COMMAND_MENU_SELECTED is correct, but the event codes are largely undocumented, and it is the only
wx.wxEVT_COMMAND_MENU* I could find.

Thanks,
Danny

Danny Shevitz wrote:

menuId = app.mainFrame.GetMenuBar().FindMenuItem('View','Show Toolbar')
newEvent = wx.CommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED, menuId)
app.mainFrame.GetEventHandler().ProcessEvent(newEvent)

this code does in fact toggle the toolbar, but it *doesn't* toggle the menu item. I can do this in code easily
enough or including Publish and Subscribe ala MVC... The question is, is there a way to trigger the event exactly as a mouse click would, including changing the check state.

A better way would be to use the EVT_UPDATE_UI event to manage the checked/unchecked state of the menu item. Just Bind it the same way you do the EVT_MENU for the item, and make the handler be something like this:

  def OnCheckShowToolbar(self, evt):
    evt.Check(bool(self.toolbarFrame))

Assuming self.toolbarFrame is either None, or the floating tool palette. When the user closes the frame then self.toolbarFrame will evaluate to False so you don't even need to worry about resetting it to None. When the menu is about to be shown then this event handler will be called and the checked state of the menu item will be set based on what you pass to event.Check().

···

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

Thanks,

your way is much better and eliminates the need for the workaround I was trying.
The frustrating part, is that I have read your book, and looked at the demos, and was completely
unaware of this. It makes me wonder how much else I'm missing...

As a style question, I keep my toolbar and either Show() it or don't. Is this better or worse style than
destroying and creating?

Danny

···

A better way would be to use the EVT_UPDATE_UI event to manage the checked/unchecked state of the menu item. Just Bind it the same way you do the EVT_MENU for the item, and make the handler be something like this:

        def OnCheckShowToolbar(self, evt):
                evt.Check(bool(self.toolbarFrame))

Assuming self.toolbarFrame is either None, or the floating tool palette. When the user closes the frame then self.toolbarFrame will evaluate to False so you don't even need to worry about resetting it to None. When the menu is about to be shown then this event handler will be called and the checked state of the menu item will be set based on what you pass to event.Check().

Danny Shevitz wrote:

Thanks,

your way is much better and eliminates the need for the workaround I was trying.
The frustrating part, is that I have read your book, and looked at the demos, and was completely
unaware of this. It makes me wonder how much else I'm missing...

Unfortunately there is a lot of things that we didn't have the space for putting into the book. We ended up way over our "page budget" as it is.

As a style question, I keep my toolbar and either Show() it or don't. Is this better or worse style than
destroying and creating?

No, that's fine. You can then use IsShown in the update ui event handler.

···

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