Save Grid Cell on change

I've seen a very recent thread on this subject but the solutions there
don't seem to work for me.

http://aspn.activestate.com/ASPN/Mail/Message/wxPython-users/2004750
recommends:

        if self.IsCellEditControlEnabled():
            self.HideCellEditControl()
            self.SaveEditControlValue()

http://aspn.activestate.com/ASPN/Mail/Message/wxPython-users/2004429
recommends:

        row = self.GetGridCursorRow()
        col = self.GetGridCursorCol()
        ctrl = self.GetCellEditor(row, col).GetControl()
        if (ctrl != None and ctrl.IsShown()):
            val = ctrl.GetLabel()
        else:
            val = self.GetCellValue(row, col)

but in either case, the behavior seems inconsistent.

rpm install of wxpython wxPythonGTK-py2.3-2.4.2.4-1

My Hybrid:

    def OnKillFocus(self, evt):
        """ When a control looses focus, it's important to save the
        value because to the end user, they think they are done editing
        """

        print "kill focus event!"
        if self.IsCellEditControlEnabled():
            self.HideCellEditControl()
            self.SaveEditControlValue()
            # self.ForceRefresh()

            row = self.GetGridCursorRow()
            col = self.GetGridCursorCol()
            ctrl = self.GetCellEditor(row, col).GetControl()
            if (ctrl != None and ctrl.IsShown()):
                val = ctrl.GetLabel()
            else:
                val = self.GetCellValue(row, col)

            print "val is %s" % val

Test Case:

1) Launch modified GridCustTable.py (attached)
2) Double Click on blank cell in last row to launch cell editor
3) Type z without hitting enter
4) click on the blank area underneath the grid
5) note stderr output:

RESULT:
   kill focus event!
   val is

EXPECTED RESULT:
   kill focus event!
   val is z

It knows the EditControl is enabled but SaveEditControlValue doesn't
seem to generate the output I expect.

Help is greatly appreciated :slight_smile:

···

--
Chris Green <cmg@dok.org>
"I'm beginning to think that my router may be confused."

GridCustTable-cmg.py (6.37 KB)

Chris Green wrote:

I've seen a very recent thread on this subject but the solutions there
don't seem to work for me.

[...]

My Hybrid:

    def OnKillFocus(self, evt):
        """ When a control looses focus, it's important to save the
        value because to the end user, they think they are done editing
        """

        print "kill focus event!"
        if self.IsCellEditControlEnabled():
            self.HideCellEditControl()
            self.SaveEditControlValue()
            # self.ForceRefresh()

            row = self.GetGridCursorRow()
            col = self.GetGridCursorCol()
            ctrl = self.GetCellEditor(row, col).GetControl()
            if (ctrl != None and ctrl.IsShown()):
                val = ctrl.GetLabel()
            else:
                val = self.GetCellValue(row, col)

            print "val is %s" % val

Test Case:

1) Launch modified GridCustTable.py (attached)
2) Double Click on blank cell in last row to launch cell editor
3) Type z without hitting enter
4) click on the blank area underneath the grid

At least part of the problem is that you are binding the EVT_KILL_FOCUS event to the grid window but you probably want to bind it to the cell editor instead. Your OnKillFocus is being invoked when the editor is created (because the GridWindow is losing focus) and so things are then getting out of sync as you hide and save and etc.

You can bind the EVT_GRID_EDITOR_CREATED event to be notified when the editor control is created and then bind something to its EVT_KILL_FOCUS at that time.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn <robin@alldunn.com> writes:

At least part of the problem is that you are binding the
EVT_KILL_FOCUS event to the grid window but you probably want to bind
it to the cell editor instead. Your OnKillFocus is being invoked when
the editor is created (because the GridWindow is losing focus) and so
things are then getting out of sync as you hide and save and etc.

You can bind the EVT_GRID_EDITOR_CREATED event to be notified when the
editor control is created and then bind something to its
EVT_KILL_FOCUS at that time.

Awesome, that worked. I'll add this info the wxGrid wiki this evening.

Thanks

···

--
Chris Green <cmg@dok.org>
You now have 14 minutes to reach minimum safe distance.