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 #-------------------------------------------------------------------------------
# Name: 模块1
# Purpose:
#
# Author: ankier
#
# Created: 14/10/2012
# Copyright: (c) ankier 2012
# Licence: <your licence>
#------------------------------------------------------------------------------- import wx, wx.grid as grd #定购的Grid cell ComboBox editor
class GridCellComboBoxEditor(grd.PyGridCellEditor):
def __init__(self, choices = []):
grd.PyGridCellEditor.__init__(self)
self.__Choices = choices def Create(self, parent, id, evtHandler):
"""
Called to create the control, which must derive from wx.Control.
*Must Override*
"""
self.__Parent = parent
self.__ComboBoxDialog = None
self.__ComboBoxButton = wx.ComboBox(parent, id, value = "", choices =self.__Choices)
self.__ComboBoxButton.SetEditable(False)
self.SetControl(self.__ComboBoxButton)
#添加新的event handler, 防止 弹出窗口后, cell 自动editor
newEventHandler = wx._core.EvtHandler()
if evtHandler:
self.__ComboBoxButton.PushEventHandler(newEventHandler)
self.__ComboBoxButton.Bind(wx.EVT_COMBOBOX, self.OnClick) def OnClick(self, event):
self.endValue = self.__ComboBoxButton.GetStringSelection() 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.__ComboBoxButton.SetDimensions(rect.x,rect.y,rect.width+2,rect.height+2,wx.SIZE_ALLOW_MINUS_ONE) def Clone(self):
"""
Create a new object which is the copy of this one
*Must Override*
"""
return GridCellComboBoxEditor() def BeginEdit(self, row, col, grid):
"""
Fetch the value from the table and prepare the edit control
to begin editing. Set the focus to the edit control.
*Must Override*
"""
self.startValue = grid.GetTable().GetValue(row, col)
self.endValue = self.startValue
self.__ComboBoxButton.SetStringSelection(self.startValue) def EndEdit(self, row, col, grid):
"""
Complete the editing of the current cell. Returns True if the value
has changed. If necessary, the control may be destroyed.
*Must Override*
"""
changed = False
if self.endValue != self.startValue:
changed = True
grid.GetTable().SetValue(row, col, self.endValue) # update the table
self.startValue = ''
return changed #定购颜色cell colour column
class GridCellComboBoxRender(grd.GridCellStringRenderer):
def __init__(self):
grd.GridCellStringRenderer.__init__(self) 转自http://www.cnblogs.com/ankier/archive/2012/10/14/2723364.html