问题
点击按钮保存wx.grid中cell的值,如果光标还在单元格中,认为还没有修改完成。此时点击按钮保存单元格中的内容,并没有更新cell中的值。
解决
# 添加一下语句
self.grid.SaveEditControlValue()
可以使用方法IsCurrentCellReadOnly()来确定当前单元格可编辑的有效性。你可以使用方法SaveEditControlValue()来确保在编辑器中所输入的新值被存储。当焦点从被编辑的单元格上移走时,网格控件隐式地调用该方法,当在你的程序中所做的一些事情可能会导致值被丢失时(比如关闭网格所处的窗口时),隐式地调用该方法是一个好的方式。
全部代码
import wx
import wx.grid
import string
class UpCaseCellEditor(wx.grid.PyGridCellEditor):
def __init__(self):
wx.grid.GridCellEditor.__init__(self)
def Create(self, parent, id, evtHandler):
"""
Called to create the control, which must derive from wx.Control.
*Must Override*
"""
self._tc = wx.TextCtrl(parent, id, "")
self._tc.SetInsertionPoint(0)
self.SetControl(self._tc)
if evtHandler:
self._tc.PushEventHandler(evtHandler)
self._tc.Bind(wx.EVT_CHAR, self.OnChar)
def SetSize(self, rect):
"""
Called to position/size the edit control within the cell rectangle.
If you don't fill the cell (the rect) then be sure to override
PaintBackground and do something meaningful there.
"""
self._tc.SetSize<