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