为什么Grid中Cell的数据没有改变,也会触发EVT_GRID_CELL_CHANGE的处理函数?

coolnick coolnick 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我是直接基于在wxPython Demo中的例子(GridCustEditor)做的实验,发现只要对Cell设置了自定义的Editor,那么即使并没有修改Cell的值,也会触发EVT_GRID_CELL_CHANGE响应函数。对于默认的没有设置自定义的Editor,则只有真正修改了Cell的数据才会触发EVT_GRID_CELL_CHANGE响应函数。有经验的高手指点一下!先谢了。代码如下:

import string

import wx
import wx.grid as gridlib

#---------------------------------------------------------------------------
class MyCellEditor(gridlib.PyGridCellEditor):
def __init__(self, log):
self.log = log
self.log.write("MyCellEditor ctor\n")
gridlib.PyGridCellEditor.__init__(self)

def Create(self, parent, id, evtHandler):
self.log.write("MyCellEditor: Create\n")
self._tc = wx.TextCtrl(parent, id, "")
self._tc.SetInsertionPoint(0)
self.SetControl(self._tc)

if evtHandler:
self._tc.PushEventHandler(evtHandler)

def SetSize(self, rect):
self.log.write("MyCellEditor: SetSize %s\n" % rect)
self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,
wx.SIZE_ALLOW_MINUS_ONE)

def Show(self, show, attr):
self.log.write("MyCellEditor: Show(self, %s, %s)\n" % (show, attr))
super(MyCellEditor, self).Show(show, attr)

def BeginEdit(self, row, col, grid):
self.log.write("MyCellEditor: BeginEdit (%d,%d)\n" % (row, col))
self.startValue = grid.GetTable().GetValue(row, col)
self._tc.SetValue(self.startValue)
self._tc.SetInsertionPointEnd()
self._tc.SetFocus()

# For this example, select the text
self._tc.SetSelection(0, self._tc.GetLastPosition())

def EndEdit(self, row, col, grid, ext):
self.log.write("MyCellEditor: EndEdit (%d,%d)\n" % (row, col))
changed = False

val = self._tc.GetValue()

if val != self.startValue:
changed = True
grid.GetTable().SetValue(row, col, val) # update the table

self.startValue = ''
self._tc.SetValue('')
return changed

def Reset(self):
self.log.write("MyCellEditor: Reset\n")
self._tc.SetValue(self.startValue)
self._tc.SetInsertionPointEnd()

def Destroy(self):
"""final cleanup"""
self.log.write("MyCellEditor: Destroy\n")
super(MyCellEditor, self).Destroy()


def Clone(self):
"""
Create a new object which is the copy of this one
*Must Override*
"""
self.log.write("MyCellEditor: Clone\n")
return MyCellEditor(self.log)

#---------------------------------------------------------------------------
class GridEditorTest(gridlib.Grid):
def __init__(self, parent, log):
gridlib.Grid.__init__(self, parent, -1)
self.log = log

self.CreateGrid(10, 3)

# but for this example, we'll just set the custom editor on one cell
self.SetCellEditor(1, 0, MyCellEditor(self.log))
self.SetCellValue(1, 0, "Try to edit this box")

# and on a column
attr = gridlib.GridCellAttr()
attr.SetEditor(MyCellEditor(self.log))
self.SetColAttr(2, attr)
self.SetCellValue(1, 2, "or any in this column")

self.SetColSize(0, 150)
self.SetColSize(1, 150)
self.SetColSize(2, 150)

self.Bind(wx.grid.EVT_GRID_CELL_CHANGE , self.OnCellChanged)

def OnCellChanged(self, evt):
"the user changes the data in a cell via an editor"
print "The user changed the value for cell(%d, %d)" % (evt.GetRow(), evt.GetCol())

#---------------------------------------------------------------------------

class TestFrame(wx.Frame):
def __init__(self, parent, log):
wx.Frame.__init__(self, parent, -1, "Custom Grid Cell Editor Test",
size=(640,480))
grid = GridEditorTest(self, log)

#---------------------------------------------------------------------------

if __name__ == '__main__':
import sys
app = wx.PySimpleApp()
frame = TestFrame(None, sys.stdout)
frame.Show(True)
app.MainLoop()

...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
coolnick
脚本语言 37,738 成员 34,208 内容