【问题标题】:wxPython segmentation fault with Editors编辑器的 wxPython 分段错误
【发布时间】:2010-07-02 20:35:07
【问题描述】:

我创建了一个带有 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

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    我发现了问题:

    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】:

      这应该解决它:

      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
      

      Obs:我不知道你为什么需要这样做,因为:

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

      上面写着:&lt;wx.grid.GridCellAttr; proxy of &lt;wx.grid.GridCellAttr&gt;...&gt;

      Obs2:如果你在整列 0 上使用 ChoiceEditor,你也可以在显示网格之前只定义一次:

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

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

      【讨论】:

      • 如果你导入 wx.grid 而不是只导入 wx,那么你不会得到属性错误。除了更改导入样式之外,我没有看到您正在做的不同的事情应该解决它。使用 SetColAttr 效果很好,并且是一种更好的方法,谢谢。但我想知道为什么会出现分段错误...
      • 是的,确切地说,解决方法是更改​​导入样式。正如您在控制台输出中看到的,如果您导入 wx 而不是 wx.grid,来自 wx.grid 的对象将不起作用。我不知道为什么,因为在导入时,该对象被列为wx.grid。无论如何,这总是这样,不仅在你的情况下。仅在使用编辑器时出现分段错误,因为它的类型错误或没有类型,因为没有使用 wx.grid 正确初始化。
      • 更改导入样式并不能解决问题。 (是的,我试过了)
      • pastebin.com/3z3PLh7a 在我应用您的修复程序后有我的代码。至少对我来说,它并不能解决我的分段错误。也许您可以尝试运行它并查看它是否在那里工作?
      • 如果你在我的回答中运行代码,我想你会发现你的 import as 技巧是不必要的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多