Introduction
This is a simple custom wxGridCellChoiceEditor sample, written after wxPython\demo\GridCustEditor.py.
What Objects are Involved
According to GridCustEditor.py, all the methods of the class must be overridden.
Process Overview
Clicking on the right-bottom cell of the grid, the custom cell-choice-editor shows up displaying not only the initial text of the cell (e.g. 'A') but also a description of it (e.g. 'A Description_of_A'). When the cell-choice-editor closes only the short name of the item is set as the text of the cell (e.g. 'A').
Code Sample
1 from wxPython.wx import *
2 from wxPython.grid import *
3
4 class CustomCellChoiceEditor(wxPyGridCellEditor):
5 def __init__(self, varDefDict, editable):
6 wxPyGridCellEditor.__init__(self)
7 self._varDefDict = varDefDict
8 self._editable = editable
9
10 def Create(self, parent, id, evtHandler):
11 """
12 Called to create the control, which must derive from wxControl.
13 """
14 self._tc = wxChoice(parent, id, choices = self._varDefDict.values())
15 if len(self._varDefDict):
16 self._tc.SetSelection(0)
17 self.SetControl(self._tc)
18 if evtHandler:
19 self._tc.PushEventHandler(evtHandler)
20
21
22 def SetSize(self, rect):
23 """
24 Called to position/size the edit control within the cell rectangle.
25 If you don't fill the cell (the rect) then be sure to override
26 PaintBackground and do something meaningful there.
27 """
28 self._tc.SetDimensions(rect.x, rect.y, rect.width+4, rect.height+4,
29 wxSIZE_ALLOW_MINUS_ONE)
30
31
32 def BeginEdit(self, row, col, grid):
33 """
34 Fetch the value from the table and prepare the edit control
35 to begin editing. Set the focus to the edit control.
36 """
37 self.startValue = grid.GetTable().GetValue(row, col)
38 self._tc.SetStringSelection(self._varDefDict[self.startValue])
39 self._tc.SetFocus()
40
41
42 def EndEdit(self, row, col, grid):
43 """
44 Complete the editing of the current cell. Returns true if the value
45 has changed. If necessary, the control may be destroyed.
46 """
47 changed = false
48
49 pclDescr = self._tc.GetStringSelection()
50 # extract pcl
51 pcl = pclDescr.split(' ')[0]
52 if pcl != self.startValue :
53 changed = true
54 grid.GetTable().SetValue(row, col, pcl) # update the table
55
56 self.startValue = ''
57 return changed
58
59
60 def Reset(self):
61 """
62 Reset the value in the control back to its starting value.
63 """
64 self._tc.SetStringSelection(self._varDefDict[self.startValue])
65
66
67 def Clone(self):
68 """
69 Create a new object which is the copy of this one
70 """
71 return CustomCellChoiceEditor(self._varDefDict, self._editable)
72
73 #---------------------------------------------------------------------------
74
75 class TestFrame(wxFrame):
76 def __init__(self, parent):
77 wxFrame.__init__(self, parent, -1, "Custom Grid Cell Editor Test",
78 size=(400,120))
79 grid = wxGrid(self, 2001, size = wxSize(360, 80))
80 grid.CreateGrid(2, 2)
81 grid.SetColLabelValue(0, 'Col 1')
82 grid.SetColLabelValue(1, 'Col 2')
83 grid.SetColLabelAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE)
84 grid.SetColSize(1, 280)
85 grid.SetRowLabelSize(0)
86 grid.SetCellValue(1, 1, 'A')
87 dict = {'A':'A Description_of_A', 'B':'B Description_of_B'}
88 grid.SetCellEditor(1, 1, CustomCellChoiceEditor(dict, true))
89
90
91 #---------------------------------------------------------------------------
92
93 if __name__ == '__main__':
94 import sys
95 app = wxPySimpleApp()
96 frame = TestFrame(None)
97 frame.Show(true)
98 app.MainLoop()
Comments
Feel free to enhance this. You may contact me at mailto:cai@xnet.ro.