代码之家  ›  专栏  ›  技术社区  ›  Winston Ewert

wxPython切分错误与编辑器

  •  3
  • Winston Ewert  · 技术社区  · 15 年前

    我用wx.grid.PyGridTableBase派生类创建了一个wx.grid.grid来提供它的数据。我还想控制表上使用的编辑器。为此,我定义了以下方法

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

    但是,每当我尝试在网格中创建编辑器时,这会导致分段错误。我确实尝试预先创建编辑器并将其作为参数传入,但收到错误:

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

    我怀疑第二个错误是由GridCellAttr取消所有权然后销毁我的编辑器引起的。

    我还尝试在wx.grid.grid上使用SetDefaultEditor方法,这是可行的,但自然不允许使用特定于列的编辑策略。

    http://pastebin.com/SEbhvaKf

    2 回复  |  直到 15 年前
        1
  •  4
  •   Winston Ewert    15 年前

    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()
    
        2
  •  0
  •   laurent    15 年前

    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:如果对整列0使用ChoiceEditor,也可以在显示网格之前仅定义一次:

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

    您可以从GetAttr方法中删除所有代码(我认为应该更快,但我从未计时)。