1

I have a menu defined using XRC like this:

<object class="wxMenuItem" name="MenuItem_ShowTooltips">
  <label>Show tooltips</label>
  <checkable>1</checkable>
</object>

which I initialize in my app from it's parent frame like this

self.frame = self.res.LoadFrame(None, 'MainFrame')

When my app starts I check a user preferences file to get initial values (a boolean in this case) and want to check or uncheck the MenuItem based on that parameter. With a normal wx control I could use something like

self.MenuItem_ShowTooltips.Check(self.UserPreferences['ShowTooltips'])

How can I get a handle on the XRC created MenuItem_ShowTooltips control?

nickvans
  • 898
  • 13
  • 24

2 Answers2

0

Well, after a couple days of fiddling, I found my answer. In case it helps anyone else, or in case I forget and end up back on StackOverflow to find the answer again, here it is.

To get a handle on the menu item you have to use GetMenuBar() method of the wx window.

self.MenuItem_ShowTooltips = self.TopWindow.GetMenuBar().FindItemById(xrc.XRCID('MenuItem_ShowTooltips'))

Once you have a handle on that you can set it's checked state like this

self.MenuItem_ShowTooltips.Check(self.UserPreferences['ShowTooltips'])

And you bind it to an event like this

self.frame.Bind(wx.EVT_MENU, self.SaveUserPrefs, id=xrc.XRCID('MenuItem_ShowTooltips'))
nickvans
  • 898
  • 13
  • 24
0

This helped me on the way to resolving my issue - thanks, although I found that I also had bind to the menu first as per the example below:

self.frameMain.Bind(wx.EVT_MENU, self.event_thatdoes_stuff, id=xrc.XRCID('name_from_xrc'))
self.mymenu_handler = self.frameMain.GetMenuBar().FindItemById(xrc.XRCID('name_from_xrc'))

Without that first line in the example above, python would not accept the line below. I wanted to add this comment for the exact same reason you did - in case it might help someone else of if I end up back looking for the answer again.
Once again, your info really helped me out and I appreciate you leaving your answer!

IamSierraCharlie
  • 504
  • 5
  • 10