wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEditor为例)
2012年10月08日 ⁄ 综合 ⁄ 共 10983字 ⁄ 字号 小 中 大 ⁄ 评论关闭
30元程序员衣装优惠券,仅剩3天!点击领取 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 附近的新行个数
) #-*-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 column类型
class GridColumnControlKind:
Text ="Text"
CheckBox = "CheckBox"
Colour = "Colour" #定购的Grid cell color editor
class GridCellColorEditor(grd.PyGridCellEditor):
def Create(self, parent, id, evtHandler):
"""
Called to create the control, which must derive from wx.Control.
*Must Override*
"""
self.__Parent = parent
self.__ColorDialog = None
self.__ColorButton = wx.Button(parent, id, "")
self.SetControl(self.__ColorButton)
#添加新的event handler, 防止 弹出窗口后, cell 自动editor
newEventHandler = wx._core.EvtHandler()
if evtHandler:
self.__ColorButton.PushEventHandler(newEventHandler)
self.__ColorButton.Bind(wx.EVT_BUTTON, self.OnClick) def OnClick(self, event):
self.__ColorButton.SetFocus()
self.ShowColorDialog() 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.__ColorButton.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 GridCellColorEditor() 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.__ColorButton.SetBackgroundColour(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 def ShowColorDialog(self):
colorDialog = wx.ColourDialog(self.__Parent)
self.__ColorDialog = colorDialog
colorDialog.GetColourData().SetColour(self.startValue)
if wx.ID_OK == colorDialog.ShowModal():
data = colorDialog.GetColourData()
colour = data.GetColour()
self.__ColorButton.SetBackgroundColour(colour)
self.endValue = colour del self.__ColorDialog
self.__ColorDialog = None #定购颜色cell colour column
class GridCellColorRender(grd.PyGridCellRenderer):
def __init__(self):
grd.PyGridCellRenderer.__init__(self) def Draw(self, grid, attr, dc, rect, row, col, isSelected):
color = grid.GetTable().GetValue(row, col)
dc.SetBrush(wx.Brush(color, wx.SOLID))
dc.SetPen(wx.TRANSPARENT_PEN)
dc.DrawRectangleRect(rect) dc.SetBackgroundMode(wx.TRANSPARENT)
def GetBestSize(self, grid, attr, dc, row, col):
# text = grid.GetCellValue(row, col)
# dc.SetFont(attr.GetFont())
# w, h = dc.GetTextExtent(text)
return wx.Size(-1, -1) def Clone(self):
return GridCellColorRender() #根据具体业务逻辑 定购grid的 table
class CustomGridTable(grd.PyGridTableBase):
def __init__(self):
grd.PyGridTableBase.__init__(self) #添加Grid column head
self.colLabels = ["Name", "Visibility", "Min threshold", "Max threshold", "Colour"]
#指定column对应的kind control
self.colControlKinds = [GridColumnControlKind.Text, GridColumnControlKind.CheckBox, GridColumnControlKind.Text, GridColumnControlKind.Text, GridColumnControlKind.Colour]
self.colControlEditorEnableStatus =[True, True, False, False, True]
self.rowLabels = ["","","","",""] #添加数据源
self.Data = [
['Mask 1', 1, "2.5","320.6",(200,20,100)]
,['Mask 2', 1, "2.5","320.6",(50,0,200)]
] def GetNumberRows(self):
return len(self.Data) def GetNumberCols(self):
return len(self.colLabels) def IsEmptyCell(self, row, col):
return False def GetValue(self, row, col):
return self.Data[row][col] def SetValue(self, row, col, value):
self.Data[row][col] = value def GetColLabelValue(self, col):
return self.colLabels[col] def GetRowLabelValue(self, row):
return self.rowLabels[row] def InsertRow(self, index, row):
if len(self.Data) < index:
return self.Data.insert(index, row)
print self.Data
self.GetView().BeginBatch() msg = grd.GridTableMessage(self,
grd.GRIDTABLE_NOTIFY_ROWS_INSERTED
,index
,1
)
self.GetView().ProcessTableMessage(msg) # ... same thing for columns .... self.GetView().EndBatch()
msg = grd.GridTableMessage(self, grd.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
self.GetView().ProcessTableMessage(msg) def DeleteRow(self, row):
rowIndex = self.Data.index(row )
if rowIndex <0:
return self.Data.remove(row) self.GetView().BeginBatch() msg = grd.GridTableMessage(self,grd.GRIDTABLE_NOTIFY_ROWS_DELETED,
rowIndex,
)
self.GetView().ProcessTableMessage(msg) # ... same thing for columns .... self.GetView().EndBatch()
msg = grd.GridTableMessage(self, grd.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
self.GetView().ProcessTableMessage(msg) def Clear(self): self.GetView().BeginBatch() msg = grd.GridTableMessage(self,grd.GRIDTABLE_NOTIFY_ROWS_DELETED,
0,
self.GetNumberCols()-1)
self.GetView().ProcessTableMessage(msg) # ... same thing for columns .... self.GetView().EndBatch() self.Data = [] msg = grd.GridTableMessage(self, grd.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
self.GetView().ProcessTableMessage(msg) def AppendRow(self, row):
self.Data.append(row)
self.GetView().BeginBatch() msg = grd.GridTableMessage(self,
grd.GRIDTABLE_NOTIFY_ROWS_APPENDED, )
self.GetView().ProcessTableMessage(msg) # ... same thing for columns .... self.GetView().EndBatch()
msg = grd.GridTableMessage(self, grd.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
self.GetView().ProcessTableMessage(msg) #对grid的功能进行封装 以便能方便的处理
class CustomGrid(grd.Grid):
def __init__(self, parent, id, rowLabelSize = 0, customGridTable = None):
grd.Grid.__init__(self, parent,id) self.RowLabelSize = rowLabelSize
self.__CustomTableSource = customGridTable
self.SetTable(self.__CustomTableSource, True) self.__InitStyle() #设置column 对应的 editor
self.__InitColumnsEditor() # self.Bind(grd.EVT_GRID_CELL_LEFT_CLICK,self.__OnMouse)
self.Bind(grd.EVT_GRID_SELECT_CELL, self.__OnCellSelected)
self.Bind(grd.EVT_GRID_EDITOR_CREATED, self.__OnEditorCreated) def __InitStyle(self):
self.SetSelectionBackground(wx.Color(237 , 145 , 33 )) def __InitColumnsEditor(self):
index = -1
for columnKind in self.__CustomTableSource.colControlKinds:
index += 1
if columnKind == GridColumnControlKind.CheckBox:
self.__InitCheckBoxColumnEditor(index)
elif columnKind == GridColumnControlKind.Colour:
self.__InitColorColumnEditor(index) def __InitCheckBoxColumnEditor(self, columnIndex):
attr = grd.GridCellAttr()
attr.SetEditor(grd.GridCellBoolEditor())
attr.SetRenderer(grd.GridCellBoolRenderer())
self.SetColAttr(columnIndex, attr) def __InitColorColumnEditor(self, columnIndex):
attr = grd.GridCellAttr()
attr.SetEditor(GridCellColorEditor())
attr.SetRenderer(GridCellColorRender())
self.SetColAttr(columnIndex, attr) def __OnCellSelected(self,evt):
if self.__CustomTableSource.colControlEditorEnableStatus[evt.Col]:
wx.CallAfter(self.EnableCellEditControl)
evt.Skip()
#设置改行为选中状态
self.SelectRow(evt.Row) def __OnEditorCreated(self, event):
pass def ForceRefresh(self):
grd.Grid.ForceRefresh(self) #主窗口
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None,title="GridTable",size=(500,200))
sizer = wx.BoxSizer(wx.HORIZONTAL)
addButton = wx.Button(self, -1, "Add")
deleteButton = wx.Button(self, -1, "Delete")
clearButton = wx.Button(self, -1, "Clear")
sizer.Add(addButton, 0, wx.SHAPED)
sizer.Add(deleteButton, 0, wx.SHAPED)
sizer.Add(clearButton, 0, wx.SHAPED) table = CustomGridTable()
grid = CustomGrid(self, id = -1, customGridTable = table)
self.__Grid = grid
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(sizer)
mainSizer.Add(grid, 1, wx.EXPAND)
self.SetSizerAndFit(mainSizer) addButton.Bind(wx.EVT_BUTTON, self.OnAddClick)
deleteButton.Bind(wx.EVT_BUTTON, self.OnDeleteClick)
clearButton.Bind(wx.EVT_BUTTON, self.OnClearClick) def OnClearClick(self, event):
table = self.__Grid.GetTable()
table.Clear()
print self.__Grid.GetTable().Data def OnDeleteClick(self, event):
table = self.__Grid.GetTable()
firstRow = table.Data[1]
table.DeleteRow(firstRow)
print self.__Grid.GetTable().Data def OnAddClick(self, event):
table = self.__Grid.GetTable()
table.InsertRow(1, ['insert index ', 1, "2.5","110.6",(50,200,30)])
print self.__Grid.GetTable().Data def main():
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop() if __name__ == '__main__':
main() #-*-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)

wx.grid的更多相关文章

  1. wx.grid.Grid

    # -*- coding: cp936 -*- import wx import wx.grid import wx.lib.gridmovers as gridmovers import pymss ...

  2. wx.grid 简单例子

    import wx, wx.grid class GridData(wx.grid.PyGridTableBase): _cols = "a b c".split() _data ...

  3. wxPython控件学习之wx.grid.Grid 表格控件

    wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...

  4. wx

    wx The classes in this module are the most commonly used classes for wxPython, which is why they hav ...

  5. 46-wxpython 4 使用 grid 展示表格

    转载:https://blog.csdn.net/soslinken/article/details/79024938#%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B wxp ...

  6. wxpython grid

    构建Grid方法,效果如下: 其它构建grid方法和grid的使用见:还可以见下载资源中的wxpython教程第5章的 gridGeneric.py gridModel.py gridNoModel. ...

  7. wxPYTHON图形化软件开发(一)---LOMO工具箱

    最近学了wxPYTHON,这次就做了一个工具箱软件练手,软件主要是包含各种小工具,目前想到的有密码管理器,日记本,记账本,今天还看到一个网页浏览器,也可能加进来.目前实现的是密码管理器 软件GUI部分 ...

  8. [ZETCODE]wxWidgets教程九:组件专题2

    本教程原文链接:http://zetcode.com/gui/wxwidgets/widgetsII/ 翻译:瓶哥 日期:2013年12月15日星期日 邮箱:414236069@qq.com 主页:h ...

  9. [wxWidgets]_[0基础]_[经常更新进度条程序]

    场景: 1. 非常根据程序的进展需要处理业务,以更新进度条,进度条的目的是为了让用户知道业务流程的进度.一个进度条程序更友好,让用户知道在程序执行.不是没有反应. 2. 现在更新见过这两种方法的进展. ...

随机推荐

  1. Qt 学习之路 2(49):自定义只读模型

    Qt 学习之路 2(49):自定义只读模型 豆子 2013年5月5日 Qt 学习之路 2 18条评论 model/view 模型将数据与视图分割开来,也就是说,我们可以为不同的视图,QListView ...

  2. Linux下parted分区超过2TB硬盘-分区格式化

    1.parted 设备名进入分区 parted /dev/vdb 2.输入print打印列出当前分区设备的磁盘容量大小 3.设置磁盘分区为gpt模 mklabel gpt 然后点击YES继续(提示磁盘 ...

  3. springMVC上传功能(单文件和多文件上传)

    单文件和多文件上传 首先在xxx-select.xml里面配置上传的大小和编码 <bean id="multipartResolver" class="org.sp ...

  4. C++_标准模板库STL概念介绍5-其他库与总结

    C++还提供了其他一些类库,这些类库更加专用. 例如,头文件complex为复数提供了类模板complex,包含用于float.long和long double的具体化. 这个类提供了标准的复数运算以 ...

  5. P4449 于神之怒加强版 (莫比乌斯反演)

    [题目链接] https://www.luogu.org/problemnew/show/P4449 给定n,m,k,计算 \(\sum_{i=1}^n \sum_{j=1}^m \mathrm{gc ...

  6. Applese 的QQ群(二分+dfs)

    链接:https://ac.nowcoder.com/acm/contest/330/F 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言5242 ...

  7. 【DP】【单调队列】洛谷 P2216 [HAOI2007]理想的正方形 题解

        算是单调队列的复习吧,不是很难 题目描述 有一个$a\times b$的整数组成的矩阵,现请你从中找出一个$n\times n$的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入 ...

  8. 腾讯云(Linux)安装.net core sdk2.1、net core runtime2.1

    按照微软指令安装: sdk2.1:https://www.microsoft.com/net/download/linux-package-manager/centos/sdk-current 1. ...

  9. VS2015打开特定项目就崩溃

    今天在打开之前写的项目的时候,一开vs就崩溃关闭了,打开其他项目的.sln和.vsproj就可以,唯独有1个项目打不开,也不知道为啥,气死了. 去网上找到的解决办法: 步骤1:开始–>所有程序– ...

  10. PIE SDK栅格数据集的读写

    1. 功能简介 栅格数据包含很多信息,在数据的运用中需要对数据的信息进行读取或写入,目前PIE SDK支持多种数据格式的数据读取和写入,下面对栅格数据格式的数据读写功能进行介绍. 2. 功能实现说明 ...