wx.grid.GridCellTextEditor 创建自定义的编辑器
默认的文本编辑器。 你可以想创建一个自定义的编辑器自行处理输入的数据。要创建你自己的编辑器,你要创建
wx.grid.PyGridCellEditor的一个子类。这比描绘器复杂些。表14.6显示了几个你需
要覆盖的方法。
表14.7显示了父类的更多的方法,你可以覆盖它们以改进你的自定义编辑器的外观。 表14.6 你必须覆盖的PyGridCellEditor的方法
BeginEdit(row, col, grid) 参数row,col是单元格的坐标,grid是包含的单元格。该方法在编辑请求之初被调用。在该方法中,编辑器用于得到数据去编辑,并为编辑做准工作。 返回该编辑器的一个拷贝。 Clone() 创建被编辑器使用的控件。参数parent是容器,id是要创建的控件的标识符,Create(parent, id, evtHandler) evtHandler是绑定到该新控件的事件处理器。 EndEdit(row, col, grid) 如果编辑已经改变了单元格的值,则返回True。任何其它的必须的清除工作都应该在这里被执行。 如果编辑被取消了,则该方法被调用。此时应该将控件中的值还原为初始值。 Reset() 表14.7 可以覆盖的PyGridCellEditor的方法
Destroy() 当编辑被销毁时,执行任何最终的清除工作。 如果evt中的键被按下会启动编辑器,则方法返回True。F2键始终都用于启动编辑器。蕨类假设任意键的按下都将启动编辑器,除非它被修改为通过control,alt,或shift来启动。 IsAcceptedKey(evt) 参数rect是一个wx.Rect(使用逻辑单位),attr是与单元格相关的PaintBackground(rect, attr) wc.grid.GridCellAttr。该方法的目的是绘制没有被编辑器控件所覆盖的单元格的部分。基类通过属性得到背景色并使用得到的背景色填充矩形。 SetSize(rect) rect是一个该控件在屏幕上的逻辑尺度的wx.Rect。如果必要的话,可以使用该方法来将控件定位在该矩形内。 参数show是一个布尔值,它决定是否显示编辑器,attr是相关单元格的属性实例。调用该方法来显示或隐藏编辑器。 当编辑器通过在单元格上的一个鼠标敲击被启动时,该方法被调用来允许编辑器将该敲击用于自己的目的。 如果编辑通过一个按键的按压被启动了,那么该方法被调用来允许编辑器控件使用该按键,如果你想的话。(例如,通过使用它作为实际编辑器的一部分)。 Show(show, attr) StartingClick() StartingKey(evt) 一旦你的编辑器完成了,你就可以使用SetCellEditor方法将它设置为任何单元格的编辑器。例14.8显示了一个自定义编辑器的示例,这个例子自动将你输入的文本转换为大写。 例14.8 创建自定义的大写编辑器
切换行号显示 1 #-*- encoding:UTF-8 -*- 2 import wx
3 import wx.grid 4 import string 5
6 class UpCaseCellEditor(wx.grid.PyGridCellEditor):#声明编辑器
7 def __init__(self):
8 wx.grid.PyGridCellEditor.__init__(self) 9
10 def Create(self, parent, id, evtHandler):#创建 11 \
12 Called to create the control, which must derive from wx.Control.
13 *Must Override* 14 \
15 self._tc = wx.TextCtrl(parent, id, \) 16 self._tc.SetInsertionPoint(0) 17 self.SetControl(self._tc) 18
19 if evtHandler:
20 self._tc.PushEventHandler(evtHandler) 21
22 self._tc.Bind(wx.EVT_CHAR, self.OnChar) 23
24 def SetSize(self, rect): 25 \
26 Called to position/size the edit control within the cell rectangle. 27 If you don't fill the cell (the rect) then be sure to override
28 PaintBackground and do something meaningful there.
29 \
30 self._tc.SetDimensions(rect.x, rect.y, rect.width+2, rect.height+2,
31 wx.SIZE_ALLOW_MINUS_ONE) 32
33 def BeginEdit(self, row, col, grid): 34 \ 35 Fetch the value from the table and prepare the edit control
36 to begin editing. Set the focus to the edit control.
37 *Must Override* 38 \
39 self.startValue = grid.GetTable().GetValue(row, col)
40 self._tc.SetValue(self.startValue) 41 self._tc.SetInsertionPointEnd() 42 self._tc.SetFocus()
43 self._tc.SetSelection(0, self._tc.GetLastPosition()) 44
45 def EndEdit(self, row, col, grid): 46 \
47 Complete the editing of the current cell. Returns True if the value
48 has changed. If necessary, the control may be destroyed.
49 *Must Override* 50 \
51 changed = False 52
53 val = self._tc.GetValue()
54
55 if val != self.startValue: 56 changed = True
57 grid.GetTable().SetValue(row, col, val) # update the table 58
59 self.startValue = '' 60 self._tc.SetValue('') 61 return changed 62
63 def Reset(self): 64 \ 65 Reset the value in the control back to its starting value.
66 *Must Override* 67 \
68 self._tc.SetValue(self.startValue) 69 self._tc.SetInsertionPointEnd() 70
71 def Clone(self): 72 \
73 Create a new object which is the copy of this one 74 *Must Override* 75 \
76 return UpCaseCellEditor() 77
78 def StartingKey(self, evt): 79 \
80 If the editor is enabled by pressing keys on the grid, this will be
81 called to let the editor do something about that first key if desired. 82 \
83 self.OnChar(evt)
84 if evt.GetSkipped():
85 self._tc.EmulateKeyPress(evt) 86
87 def OnChar(self, evt): 88 key = evt.GetKeyCode() 89 if key 255: 90 evt.Skip() 91 return
92 char = chr(key)
93 if char in string.letters:
94 char = char.upper()
95 self._tc.WriteText(char)#转换为大写 96 else:
97 evt.Skip() 98
99 class TestFrame(wx.Frame): 100 def __init__(self):
101 wx.Frame.__init__(self, None, title=\Editor\,
102 size=(640,480)) 103
104 grid = wx.grid.Grid(self) 105 grid.CreateGrid(50,50)
106 grid.SetDefaultEditor(UpCaseCellEditor())#设置成默认编辑器 107 108
109 app = wx.PySimpleApp() 110 frame = TestFrame() 111 frame.Show() 112 app.MainLoop()
捕获用户事件
网格控件有许多的用户事件。我们将把它们分为鼠标事件和键盘事件。
如何捕获用户的鼠标动作?
对于网格控件,除了不同的鼠标事件类型外,对于这些类型还有一些不同的事件类。最常用的事件类是wx.grid.GridEvent。网格的事件类是wx.CommandEvent的一个子类,它提供了用于获得事件详情的几个方法,如表14.8所示。 表14.8 wx.grid.GridEvent的方法
AltDown() 当事件被触发时,如果alt键被按下了,则返回True。 ControlDown() 当事件被触发时,如果control键被按下了,则返回True。 GetCol() GetPosition() GetRow() 返回发生事件的单元格所在的列的索引。 返回返回一个wx.Point。它代表事件发生点的逻辑坐标(以像素为单位)。 返回发生事件的单元格所在的行的索引。