menuitem.SetCheckable / Check doesn't seem to work (always) ?

hello,

For popup menus, I always use standard menu items (so not checkable).
The reason for this is that they are generated by some convenience routine (see below).
Now if I need a checkable menuitem,
I've a convenience procedure that changes item.Checkable and item.Check.
If the menu is generated static (in de form creation), it seems to work.
But if I do this dynamically in the popup event, it doesn't seem to work.
I'm not sure if the static/dynamic is the real reason.
At least the code below doesn't seem to work.

Is it allowed to use SetCheckable, to change a menuitem from normal to checkable ?
Is it allowed to perform such actions in the popup event itself ?
Other suggestions ?

thanks,
Stef Mientki

  # *****************************************************************
  def _OnShowPopup ( self, event ) :

    self.Popup_Menu = wx.Menu ()
    item = self.Popup_Menu.Append ( 'test' )

    My_Value = True
    # Doesn't work
    item.SetCheckable ( My_Value )
    item.Check = My_Value

    self.Hit_Pos = self.ScreenToClient ( event.GetPosition () )
    self.PopupMenu ( self.Popup_Menu, pos = self.Hit_Pos )
       self.Popup_Menu.Destroy()

Convenience routine:
# ***********************************************************************
class My_Popup_Menu ( wx.Menu ) :

  default = {}

  # edit
  default[0] = [
      _(0,'Cut'), _(0,'Copy'), _(0,'Paste'), _(0,'Delete')]

  # extended edit
  default[1] = [
      '-', _(0,'Cut'), _(0,'Copy'), _(0,'Paste'), _(0,'Delete'), '-' ]

  # tree
  default[2] = [
      _(0,'Insert New\tIns'), _(0,'Edit\tSpace'),
      '-', _(0,'Cut\tCtrl-X'), _(0,'Copy\tCtrl-C'),
      _(0,'Paste\tCtrl-V'), _(0,'Delete\tDel') ]

  # *************************************************************
  # itemset = None, doesn't use a default set
  # *************************************************************
  def __init__ ( self, OnSelect, itemset = 0, pre = None, after = None ):
    wx.Menu.__init__ ( self )

    # Determine the itemlist = pre + set + after
    items = []
    if pre :
      items = pre
    if isinstance ( itemset, int ) :
      items += self.default [ itemset ]
    if after :
      items += after

    # generate the menu
    self.IDs = []
    self.items = []
    for text in items :
      if text == '-' :
        item = self.AppendSeparator()
      else :
        item = self.Append (wx.ID_ANY, text )
        # order of these bindings is important
        # to be sure we get it first
        self.Bind ( wx.EVT_MENU, OnSelect, item )
        self.Bind ( wx.EVT_MENU, self.OnSelect, item )
        # only save IDs of real items
        self.IDs.append ( item.GetId() )
        self.items.append ( item )

  # *************************************************************
  def Get_Index_by_ID ( self, ID_sel ) :
    for i, ID in enumerate ( self.IDs ) :
      if ID == ID_sel :
        break
    return i

  # *************************************************************
  def Get_Item_by_ID ( self, ID_sel ) :
    i = self.Get_Index_by_ID ( ID_sel )
    return self.items [i]

  # *************************************************************
  def OnSelect ( self, event ) :
    ID_sel = event.GetId ()
    for i, ID in enumerate ( self.IDs ) :
      if ID == ID_sel :
        break
    # return the index
    # event.Int is only used in very special cases (what?)
    # so it's valid to use it here to transport the index
    event.Int = i
    event.Skip ()

  # *************************************************************
  def SetEnabled ( self, index, value = True ) :
    ID = self.IDs [ index ]
    value = bool ( value )
    self.Enable ( ID, value )

  # *************************************************************
  def SetChecked ( self, index, value = True ) :
    item = self.items [ index ]
    if value :
      item.SetCheckable ( True )
      item.Check = value
    else :
      item.SetCheckable ( False )
    #print ' SETCHECK',index,value, item, \
    # item.IsCheckable(),item.Check,\
    # self.GetLabelText(self.IDs[index])
# ***********************************************************************

Robin Dunn wrote:

Stef Mientki wrote:

hello,

For popup menus, I always use standard menu items (so not checkable).
The reason for this is that they are generated by some convenience routine (see below).
Now if I need a checkable menuitem,
I've a convenience procedure that changes item.Checkable and item.Check.
If the menu is generated static (in de form creation), it seems to work.
But if I do this dynamically in the popup event, it doesn't seem to work.
I'm not sure if the static/dynamic is the real reason.
At least the code below doesn't seem to work.

Is it allowed to use SetCheckable, to change a menuitem from normal to checkable ?

Not after it has been added to the menu.

Could it be possible, that your are mistaken Robin ?

After getting some more weird results,
I found the error in my code:
   item.Check = True
which should have been:
   item.Check ( True )

Maybe the name "Check" is not well chosen and would better be "SetCheck" ?

Now everything seems to work as expected:

For any popup menu, I can create normal items always:
        item = self.Append (wx.ID_ANY, text )

At any location in my program (also in the contextmenu event),
I can change a normal menuitem into a checkable item by
      item.SetCheckable ( True )

I can check an item before showing the popup by
      item.Check ( True )

cheers,
Stef