SEGFAULT与自定义wx.PyGridCellEditor

时间:2013-02-04 08:55:00

标签: python wxpython wxwidgets

我正在尝试创建一个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()

谁能告诉我哪里出错?

1 个答案:

答案 0 :(得分:1)

Create方法中,您需要调用self.SetControl(self.combo)让编辑器基类知道什么是真正的控件。您还应该self.combo.PushEventHandler(evtHandler),以便设置与网格的正确交互。