wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEditor为例)
wx.Grid 及其相关的类是用来显示和编辑类表格样式的数据。该控件为显示,编辑数据源提及交互供了丰富的特征。
wx.GridTableBase类控制要显示的实际数据。可以call CreateGrid()产生一个该类的实例对象。
wx.GridCellRenderer 基类,负责对单元格进行绘画。现在提供了默认的几种派生。
- wxGridCellBoolRenderer 显示CheckBox样式
- wxGridCellFloatRenderer
- wxGridCellNumberRenderer
- wxGridCellStringRenderer
wx.GridCellEditor 基类,负责在cell editing状态下,显示对应的控件。现在提供了默认的几种派生。
- wxGridCellBoolEditor
- wxGridCellChoiceEditor
- wxGridCellFloatEditor
- wxGridCellNumberEditor
- wxGridCellTextEditor
如何添加、删除行,列和单元格?
本例中使用SetTable() 作为grid 的数据源。 那么重点来研究在这个情况下如何对grid 和 数据源进行增删改。
GridTableMessage 类,可以用来向表发送一些message,本例中 是对行的增删改操作, 那么我们只需要用其中的3个message:
GRIDTABLE_NOTIFY_ROWS_INSERTED 行插入的消息
GRIDTABLE_NOTIFY_ROWS_APPENDED
附近新行的消息
GRIDTABLE_NOTIFY_ROWS_DELETED 删除行的消息
GRIDTABLE_REQUEST_VIEW_GET_VALUES cell 值如果有更改的消息
1.在index插入一新行
grd.GridTableMessage(self,
grd.GRIDTABLE_NOTIFY_ROWS_INSERTED
,index
改行所在的索引
,1 插入一行记录
)
2. 删除rowIndex行
grd.GridTableMessage(self,grd.GRIDTABLE_NOTIFY_ROWS_DELETED,
rowIndex,
改行所在的索引
1 只删除一行
)
3. 附加一新行
grd.GridTableMessage(self,
grd.GRIDTABLE_NOTIFY_ROWS_APPENDED,
1
附近的新行个数
)
1 #-*-coding:utf-8 2 3 #------------------------------------------------------------------------------- 4 # Name: 模块1 5 # Purpose: 6 # 7 # Author: ankier 8 # 9 # Created: 14/10/2012 10 # Copyright: (c) ankier 2012 11 # Licence: <your licence> 12 #------------------------------------------------------------------------------- 13 14 import wx, wx.grid as grd 15 16 #定购的Grid cell ComboBox editor 17 class GridCellComboBoxEditor(grd.PyGridCellEditor): 18 def __init__(self, choices = []): 19 grd.PyGridCellEditor.__init__(self) 20 self.__Choices = choices 21 22 def Create(self, parent, id, evtHandler): 23 """ 24 Called to create the control, which must derive from wx.Control. 25 *Must Override* 26 """ 27 self.__Parent = parent 28 self.__ComboBoxDialog = None 29 self.__ComboBoxButton = wx.ComboBox(parent, id, value = "", choices =self.__Choices) 30 self.__ComboBoxButton.SetEditable(False) 31 self.SetControl(self.__ComboBoxButton) 32 #添加新的event handler, 防止 弹出窗口后, cell 自动editor 33 newEventHandler = wx._core.EvtHandler() 34 if evtHandler: 35 self.__ComboBoxButton.PushEventHandler(newEventHandler) 36 self.__ComboBoxButton.Bind(wx.EVT_COMBOBOX, self.OnClick) 37 38 39 def OnClick(self, event): 40 self.endValue = self.__ComboBoxButton.GetStringSelection() 41 42 43 def SetSize(self, rect): 44 """ 45 Called to position/size the edit control within the cell rectangle. 46 If you don‘t fill the cell (the rect) then be sure to override 47 PaintBackground and do something meaningful there. 48 """ 49 self.__ComboBoxButton.SetDimensions(rect.x,rect.y,rect.width+2,rect.height+2,wx.SIZE_ALLOW_MINUS_ONE) 50 51 def Clone(self): 52 """ 53 Create a new object which is the copy of this one 54 *Must Override* 55 """ 56 return GridCellComboBoxEditor() 57 58 def BeginEdit(self, row, col, grid): 59 """ 60 Fetch the value from the table and prepare the edit control 61 to begin editing. Set the focus to the edit control. 62 *Must Override* 63 """ 64 self.startValue = grid.GetTable().GetValue(row, col) 65 self.endValue = self.startValue 66 self.__ComboBoxButton.SetStringSelection(self.startValue) 67 68 def EndEdit(self, row, col, grid): 69 """ 70 Complete the editing of the current cell. Returns True if the value 71 has changed. If necessary, the control may be destroyed. 72 *Must Override* 73 """ 74 changed = False 75 if self.endValue != self.startValue: 76 changed = True 77 grid.GetTable().SetValue(row, col, self.endValue) # update the table 78 self.startValue = ‘‘ 79 return changed 80 81 82 83 #定购颜色cell colour column 84 class GridCellComboBoxRender(grd.GridCellStringRenderer): 85 def __init__(self): 86 grd.GridCellStringRenderer.__init__(self) 转自http://www.cnblogs.com/ankier/archive/2012/10/14/2723364.html