I have created a wx.grid.Grid with a wx.grid.PyGridTableBase derived class to provide its data. I want to also to control the editors used on the table. Towards that end I defined the following method
def GetAttr(self, row, col, kind):
attr = wx.grid.GridCellAttr()
if col == 0:
attr.SetEditor( wx.grid.GridCellChoiceEditor() )
return attr
However, this causes a segmentation fault whenever I attempt to created the editor in the grid. I did try creating the editor beforehand and passing it in as a parameter but received the error:
TypeError: in method 'GridCellAttr_SetEditor', expected argument 2 of type
'wxGridCellEditor *'
I suspect the second error is caused by the GridCellAttr taking ownership off and then destroying my editor.
I also tried using the SetDefaultEditor method on the wx.grid.Grid and that works, but naturally does not allow me to have a column specific editing strategy.
See Full Example of crashing program: http://pastebin.com/SEbhvaKf
I figured out the problem:
The wxWidgets code assumes that the same Editor will be consistently returned from GetCellAttr. Returning a different editor each time as I was doing caused the segmentation faults.
In order to return the same editor multiple times I also need to call IncRef() on the editor to keep it alive.
For anybody else hitting the same problem in future, see my working code:
import wx.grid
app = wx.PySimpleApp()
class Source(wx.grid.PyGridTableBase):
def __init__(self):
super(Source, self).__init__()
self._editor = wx.grid.GridCellChoiceEditor()
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
return repr( (row, col) )
def SetValue(self, row, col, value):
pass
def GetNumberRows(self):
return 5
def GetNumberCols(self):
return 5
def GetAttr(self, row, col, kind):
attr = wx.grid.GridCellAttr()
self._editor.IncRef()
attr.SetEditor( self._editor )
return attr
frame = wx.Frame(None)
grid = wx.grid.Grid(frame)
grid.SetTable( Source() )
frame.Show()
app.MainLoop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With