【问题标题】:wxpython PyGridTableBasewxpython PyGridTableBase
【发布时间】:2010-03-16 10:01:51
【问题描述】:

我想使用带有 PyGridTableBase 的可编辑选择编辑器。当我编辑 崩溃的单元格 我的代码有什么错误? 我的python代码:

import wx
import wx.grid as gridlib

class MyTableBase(gridlib.PyGridTableBase):

   def __init__(self):
       gridlib.PyGridTableBase.__init__(self)

       self.data = {0:["value 1", "value 2"],
                    1:["value 3", "value 4", "value 5"]}

       self.column_labels = [unicode(u"Label 1"),
                             unicode(u"Label 2"),
                             unicode(u"Label 3")]



       self._rows = self.GetNumberRows()
       self._cols = self.GetNumberCols()

   def GetColLabelValue(self, col):
       return self.column_labels[col]

   def GetNumberRows(self):
       return len(self.data.keys())

   def GetNumberCols(self):
       return len(self.column_labels)

   def GetValue(self, row, col):
       try:
           if col > self.GetNumberCols():
               raise IndexError
           return self.data[row][col]
       except IndexError:
           return None

   def IsEmptyCell(self, row, col):
       if self.data[row][col] is not None:
           return True
       else:
           return False

   def GetAttr(self, row, col, kind):
       attr = gridlib.GridCellAttr()
       editor = gridlib.GridCellChoiceEditor(["xxx", "yyy", "zzz"],
                                             allowOthers = True)
       attr.SetEditor(editor)
       attr.IncRef()
       return attr

class MyDataGrid(gridlib.Grid):
   def __init__(self, parent):
       gridlib.Grid.__init__(self, parent, wx.NewId())
       self.base_table = MyTableBase()
       self.SetTable(self.base_table)


if __name__ == '__main__':
   app = wx.App(redirect = False)
   frame = wx.Frame(None, wx.NewId(), title = u"Test")
   grid_ = MyDataGrid(frame)
   frame.Show()
   app.MainLoop()

【问题讨论】:

    标签: wxpython


    【解决方案1】:

    我在这里遇到了同样的问题:

    wxPython segmentation fault with Editors

    基本上,您必须在每次调用 GetAttr 时设置相同的编辑器实例,而不是每次都创建一个新的编辑器对象。

    【讨论】:

      【解决方案2】:

      这个例子很好用

      http://wiki.wxpython.org/GridCellChoiceEditor

      这不是你需要的吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多