problem with check box in a grid

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

Please provide a runnable sample that demonstrates the problem. MakingSampleApps - wxPyWiki

···

On 11/8/11 3:20 AM, Hanss wrote:

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:

--
Robin Dunn
Software Craftsman

Thank you for your answer. This is my code.

Gabriele

ErrorGrid.py (4.65 KB)

Hanss wrote:

Thank you for your answer. This is my code.

In the following line -
    v = ["1", "0", "Description"]

  you are passing in an initial value of "0" to the boolean column.

By default, the values used by GridCellBoolEditor are "1" for 'checked' and
"" for 'unchecked'. [1]

If you change the second argument from "0" to "", it works.

Frank Millman

[1] You can change the default with UseStringValues() if you need to.

Thank you very much, Frank!

···

On 9 Nov, 06:32, "Frank Millman" <fr...@chagford.com> wrote:

By default, the values used by GridCellBoolEditor are "1" for 'checked' and
"" for 'unchecked'. [1]

If you change the second argument from "0" to "", it works.

[1] You can change the default with UseStringValues() if you need to.