我正在尝试创建一个允许在运行时更改选项列表的 GridCellChoiceEditor 版本。原因是要呈现的选项是对我的数据进行查询的结果,因此每次使用编辑器时它们可能都会改变。我这样做是为了继承 PyGridCellEditor,但是每当我运行它时,它会在创建编辑器后立即出现段错误。这是我的代码,为了测试使用静态列表和只有一个单元格而进行了简化:
import wx
import wx.grid
class ListEditor(wx.grid.PyGridCellEditor):
def __init__(self, options):
super(ListEditor, self).__init__()
self.options = options
def ApplyEdit(self, row, col, grid):
grid.SetValue(row, col, self.value)
def BeginEdit(self, row, col, grid):
print('begin edit')
value = grid.GetValue(row, col)
index = self.options.index(value)
self.combo.SetOptions(self.options)
self.combo.SetIndex(index)
def Create(self, parent, id, evtHandler):
self.combo = wx.ComboBox(parent, id)
print('combo created')
def Clone(self):
return ListEditor(self.options)
def EndEdit(self, row, col, grid, oldval, newval):
if oldval == newval:
return False
else:
self.value = newval
return True
def Reset(self):
pass
def GetValue(self):
return 'a'
class F(wx.Dialog):
def __init__(self):
super(F, self).__init__(None)
self.grid = wx.grid.Grid(self, -1, (0, 0), (300, 300))
self.grid.CreateGrid(1, 1)
editor = ListEditor(['a', 'b', 'c'])
self.grid.SetCellEditor(0, 0, editor)
app = wx.App(False)
f = F()
f.Show()
app.MainLoop()
谁能告诉我哪里出错了?