aquavitae
aquavitae

Reputation: 19114

SEGFAULT with a custom wx.PyGridCellEditor

I am trying to create a version of a GridCellChoiceEditor which allows the list of choices to be changed at runtime. The reason is that the choices to be presented are the result of a query on my data, so they will probably change every time the editor is used. I am doing this be subclassing PyGridCellEditor, but whenever I run it, it segfaults immediately after creating the editor. Here is my code, simplified for the purposes of testing to use a static list and only one cell:

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()

Can anyone tell me where I'm going wrong?

Upvotes: 1

Views: 215

Answers (1)

RobinDunn
RobinDunn

Reputation: 6306

In your Create method you need to call self.SetControl(self.combo) to let the editor base class know what is the real control. You should also do self.combo.PushEventHandler(evtHandler) so the proper interactions with the grid are set up.

Upvotes: 1

Related Questions