WxPython segmentation error with editors

I created wx.grid.Grid with a wx.grid.PyGridTableBase derived class to provide my data. I also want to control the editors used in the table. To this end, I have 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 error whenever I try to create an editor in a grid. I tried to create an editor in advance and pass it as a parameter, but received an error:

    TypeError: in method 'GridCellAttr_SetEditor', expected argument 2 of type 
'wxGridCellEditor *'

I suspect that the second error was caused by disabling GridCellAttr and destroying my editor.

I also tried using the SetDefaultEditor method in wx.grid.Grid, and this works, but naturally does not allow me to have a column editing strategy.

See the complete crash program example: http://pastebin.com/SEbhvaKf

+3
2

:

wxWidgets , GetCellAttr. , , , .

, IncRef() , .

, - , . :

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

:

import wx
import wx.grid as gridlib

:

def GetAttr(self, row, col, kind):
    attr = gridlib.GridCellAttr()
    if col == 0:
        attr.SetEditor( gridlib.GridCellChoiceEditor() )
    return attr

: , , :

>>> import wx
>>> attr = wx.grid.GridCellAttr()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'grid'

, :

import wx.grid as gridlib
attr = gridlib.GridCellAttr()

... :

print attr
<wx.grid.GridCellAttr; proxy of <wx.grid.GridCellAttr; proxy of <Swig Object of type 'wxGridCellAttr *' at 0x97cb398> > >

: <wx.grid.GridCellAttr; proxy of <wx.grid.GridCellAttr>...>!

Obs2: ChoiceEditor 0, , :

attr.SetEditor( gridlib.GridCellChoiceEditor() )
yourGrid.SetColAttr(0, attr)

GetAttr ( , , ).

0

Source: https://habr.com/ru/post/1752898/


All Articles