Hello,
I need to use a check box in a wxGrid cell. I want to change my
Boolean value on every click. I found a very good example for that and
I tried to implement my code. But the first click generates an error
“Invalid value for a cell with bool editor” in wxGridBoolEditor. I
can’t understand why… in the beginning the cell values “0” and when I
click it apparently changes in “1”. And from the second click, it
works. It sounds like a problem of “timing” problem… but I can’t find
the solution… any advices?? Thank you
This is my code:
attr = wx.grid.GridCellAttr()
attr.SetEditor(wx.grid.GridCellBoolEditor())
attr.SetRenderer(wx.grid.GridCellBoolRenderer())
self.SetColAttr(col,attr)
self.SetColSize(1,20)
self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK,self.onMouse)
self.Bind(wx.grid.EVT_GRID_SELECT_CELL,self.onCellSelected)
self.Bind(wx.grid.EVT_GRID_EDITOR_CREATED,
self.onEditorCreated)
def onMouse(self,evt):
if evt.Col == 1:
wx.CallLater(1000,self.toggleCheckBox)
evt.Skip()
def toggleCheckBox(self):
self.cb.Value = not self.cb.Value
self.afterCheckBox(self.cb.Value)
def onCellSelected(self,evt):
if evt.Col == 1:
wx.CallAfter(self.EnableCellEditControl)
evt.Skip()
def onEditorCreated(self,evt):
if evt.Col == 1:
self.cb = evt.Control
self.cb.WindowStyle |= wx.WANTS_CHARS
self.cb.Bind(wx.EVT_KEY_DOWN,self.onKeyDown)
self.cb.Bind(wx.EVT_CHECKBOX,self.onCheckBox)
evt.Skip()
def onKeyDown(self,evt):
if evt.KeyCode == wx.WXK_UP:
if self.GridCursorRow > 0:
self.DisableCellEditControl()
self.MoveCursorUp(False)
elif evt.KeyCode == wx.WXK_DOWN:
if self.GridCursorRow < (self.NumberRows-1):
self.DisableCellEditControl()
self.MoveCursorDown(False)
elif evt.KeyCode == wx.WXK_LEFT:
if self.GridCursorCol > 0:
self.DisableCellEditControl()
self.MoveCursorLeft(False)
elif evt.KeyCode == wx.WXK_RIGHT:
if self.GridCursorCol < (self.NumberCols-1):
self.DisableCellEditControl()
self.MoveCursorRight(False)
else:
evt.Skip()
def onCheckBox(self,evt):
self.afterCheckBox(evt.IsChecked())
def afterCheckBox(self,isChecked):
print 'afterCheckBox',self.GridCursorRow,isChecked
self.isChecked = isChecked