wxpython wxgrid属性

时间:2015-06-13 13:55:19

标签: grid attributes wxpython attr

我在网格中突出显示替代行时遇到问题。创建网格并用数据填充后,突出显示替代行的工作方式符合预期。但是当加载新数据时,我会删除所有行,然后根据需要添加新行,这次网格突出显示会引发异常未处理的TypeError。这让我难过 - 有什么建议吗? 下面的代码产生相同的错误(单击按钮两次): -

import wx
import wx.grid as gridlib
app = wx.App()

def highlightrows(event):
   for row in range(0, myGrid.GetNumberRows(), 2):
      if row < myGrid.GetNumberRows():
         myGrid.SetRowAttr(row, attr)
   myGrid.ForceRefresh()
   myGrid.Refresh()

frame = wx.Frame(None, title="Highlight woes")
panel = wx.Panel(frame)
myGrid = gridlib.Grid(panel)
myGrid.CreateGrid(12, 8)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(myGrid, 1, wx.EXPAND)
panel.SetSizer(sizer)

btn = wx.Button(panel,  -1, 'Highlight rows')
sizer.Add(btn)
btn.Bind(wx.EVT_BUTTON, highlightrows)

attr = wx.grid.GridCellAttr()
attr.SetBackgroundColour('#eeeeee')

frame.Show()
app.MainLoop()

3 个答案:

答案 0 :(得分:0)

在您的示例中,您的GridCellAttr实例已超出范围,并在第二次按下按钮时被删除。老实说,我很惊讶它第一次工作。当我单击按钮两次时,出现以下错误:

TypeError: in method 'Grid_SetRowAttr', expected argument 3 of type 'wxGridCellAttr *'

深入挖掘一下,如果你看一下第三个参数,你会发现它是如下:

<wx.grid.GridCellAttr; proxy of wxPython wrapper for DELETED GridCellAttr object! (The C++ object no longer exists.) >

无论如何,这里有一个简单的方法,每次都适用于我:

import wx
import wx.grid as gridlib

########################################################################
class MyForm(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="A Simple Grid")
        panel = wx.Panel(self)

        self.myGrid = gridlib.Grid(panel)
        self.myGrid.CreateGrid(12, 8)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.myGrid, 1, wx.EXPAND)

        btn = wx.Button(panel, label='Highlight rows')
        sizer.Add(btn)
        btn.Bind(wx.EVT_BUTTON, self.highlightrows)

        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def highlightrows(self, event):
        """"""
        attr = wx.grid.GridCellAttr()
        attr.SetBackgroundColour('#eeeeee')
        for row in range(0, self.myGrid.GetNumberRows(), 2):
            if row < self.myGrid.GetNumberRows():
                self.myGrid.SetRowAttr(row, attr)
        self.myGrid.ForceRefresh()
        self.myGrid.Refresh()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm().Show()
    app.MainLoop()

答案 1 :(得分:0)

很奇怪 - 代码在wxPython 2.8中运行良好的类似问题。升级到3.0后,我不得不克隆()每行的属性,否则我会得到上面的类型错误:

TypeError: in method 'Grid_SetRowAttr', expected argument 3 of type 'wxGridCellAttr *'

感谢Jim和Lawrie提出的建议。这可能是个错误吗?这是我的代码:

def initialPaint(self):
    wx.lib.colourdb.updateColourDB()
    self.oddLineColour = wx.NamedColour('GAINSBORO')
    oddlineattr = wx.grid.GridCellAttr()
    oddlineattr.SetBackgroundColour(self.oddLineColour)

    for (j, header) in enumerate(self.columnList):
        self.SetColLabelValue(j, header)
        for (i, bond) in enumerate(self.bondList):
            if bond in self.bdm.df.index:
                if i % 2:
                    self.SetRowAttr(i,oddlineattr.Clone())#NOT NEEDED IN 2.8

答案 2 :(得分:-1)

有点晚了,试试这个。我有同样的问题...

myGrid.SetRowAttr(row, attr.Clone())