I have this (I left out code that is irrelevant):
class TaskBarIcon(wx.TaskBarIcon):
def __init__(self, frame):
self.frame = frame
super(TaskBarIcon, self).__init__()
def CreatePopupMenu(self):
self.menu = wx.Menu()
self.menu.chk = self.menu.Append(101, 'Show statubar', 'Show Statusbar', kind=wx.ITEM_CHECK)
self.menu.Check(101, True)
self.Bind(wx.EVT_MENU, self.on_tog, id=101)
return self.menu
Which works, and the check on the menuitem is there. This is a system tray icon in Linux with a right-click menu.
But when I do:
def on_tog(self, event):
self.menu.Check(self.menu.chk.GetId(), False) #Uncheck running box
It doesn't uncheck itself. Even if I replace the GetID() with the actual ID of 101, it still doesn't uncheck.
What am I missing here? There's not much documentation on changing this other than what I did, but it's not working.
I got it to work by using AppendCheckItem
self.menu.chk = self.menu.AppendCheckItem(101, 'Show statubar', 'Show Statusbar')
The rest of the code should work as expected.
Update
Here are the relevant parts of my code in their entirety
MENU_ITEM_ID = wx.NewId()
...
self.menu_item = self.menu.AppendCheckItem(MENU_ITEM_ID, "Test Menu Item")
...
self.menu.Bind(wx.EVT_MENU, self.toggle_menuitem, id=MENU_ITEM_ID)
...
def toggle_menuitem(self, event):
self.menu_item.Check(self.menu_item.IsChecked() == False) # toggle checkmark
Update #2 *
After an extended discussion in the comments, it was determined the OP had the CreatePopupMenu
method bound to a right click event. This re-created the wx.Menu every time and thus erased any state that was assigned to the previous menu. To workaround this simply create a flag from the parent class that tracks the checkmark value and call self.menu_item.Check(self.is_checked)
after creating the popup menu to set it to the correct state.