class GridCellChoiceEditor(wx.grid.PyGridCellEditor):
def __init__(self):
wx.grid.PyGridCellEditor.__init__(self)
def Create(self, parent, id, evtHandler):
choicesList = ['edit me', 'to select', 'between choices']
self._choiceCtrl = wx.Choice(parent, id, choices=choicesList)
self.SetControl(self._choiceCtrl)
if evtHandler:
self._choiceCtrl.PushEventHandler(evtHandler)
def SetSize(self, rect):
self._choiceCtrl.SetDimensions(rect.x, rect.y,
rect.width+2, rect.height+2, wx.SIZE_AUTO)
def BeginEdit(self, row, col, grid):
self._startValue = grid.GetTable().GetValue(row, col)
self._choiceCtrl.SetStringSelection(self._startValue)
self._choiceCtrl.SetFocus()
def EndEdit(self, row, col, grid, oldVal):
val = self._choiceCtrl.GetStringSelection()
if val != self._startValue:
return val
else:
return None
def ApplyEdit(self, row, col, grid):
val = self._choiceCtrl.GetStringSelection()
grid.GetTable().SetValue(row, col, val)
def Reset(self):
self._choiceCtrl.SetStringSelection(self._startValue)
def Clone(self):
return GridCellChoiceEditor()
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY)
self.testGrid = wx.grid.Grid(self)
self.testGrid.CreateGrid(5, 5)
self.testGrid.SetSelectionMode(0)
self.testGrid.SetCellValue(0, 0, 'edit me')
self.testGrid.SetCellValue(1, 0, 'to select')
self.testGrid.SetCellValue(2, 0, 'between choices')
cellAttr = wx.grid.GridCellAttr()
cellAttr.SetEditor(GridCellChoiceEditor())
self.testGrid.SetColAttr(0, cellAttr)
szr = wx.BoxSizer(wx.VERTICAL)
szr.Add(self.testGrid, 1, wx.EXPAND)
self.SetSize((300, 200))
self.SetSizer(szr)
self.Layout()
self.Centre(wx.BOTH)