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

coolnick 2013-08-13 02:26:16
我是直接基于在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()

...全文
306 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolnick 2013-08-14
  • 打赏
  • 举报
回复
Grid一共有3列,前两列使用的是默认的editor,值改变时才触发EVT_GRID_CELL_CHANGE,而第三列则使用了自定义的editor,不管值有没有改变,只要你激活了editor,都会触发EVT_GRID_CELL_CHANGE。 你试试第3列...
panghuhu250 2013-08-14
  • 打赏
  • 举报
回复
改了以后没有错误了,运行起来也像预期的一样:值改变时才触发EVT_GRID_CELL_CHANGE。
coolnick 2013-08-14
  • 打赏
  • 举报
回复
“TypeError: EndEdit() takes exactly 5 arguments (4 given)”,意思是 EndEdit需要5个参数,而实际只给了4个参数。 其实例子EndEdit原本只有4个参数,定义如下: def EndEdit(self, row, col, grid): 由于我使用的Portable Python 2.7,EndEdit就需要5个参数了,我就在后面加了一个ext,改为: def EndEdit(self, row, col, grid, ext): 估计你没有做任何修改,直接运行的GridCustEdit例子的代码吧?在后面加一个ext。
panghuhu250 2013-08-13
  • 打赏
  • 举报
回复
得到这个:TypeError: EndEdit() takes exactly 5 arguments (4 given)

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧