当wxGrid中的单元格以编程方式更改时,高亮显示该行(使用wxPython)

当wxGrid中的单元格以编程方式更改时,高亮显示该行(使用wxPython),python,wxpython,Python,Wxpython,更新基础PyGridTableBase后如何突出显示行?我似乎无法理解它 我能够在第一次绘制表格时创建备用行高亮显示线: ### Style the table a little. def GetAttr(self, row, col, prop): attr = gridlib.GridCellAttr() if self.is_header(row): attr.SetReadOnly(1) attr.SetF

更新基础PyGridTableBase后如何突出显示行?我似乎无法理解它

我能够在第一次绘制表格时创建备用行高亮显示线:

### Style the table a little.
def GetAttr(self, row, col, prop):
        attr = gridlib.GridCellAttr()

        if self.is_header(row):
            attr.SetReadOnly(1)
            attr.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL))
            attr.SetBackgroundColour(wx.LIGHT_GREY)
            attr.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
            return attr

        if prop is 'highlight':
             attr.SetBackgroundColour('#14FF63')
             self.SetRowAttr(attr, row)


        # Odd Even Rows
        if row%2 == 1:
            attr.SetBackgroundColour('#CCE6FF')
            return attr
        return None
但当一只偶数的火点燃时就不会了


非常感谢您的帮助。

您所做的事情是正确的,唯一想到的问题是您可能没有在
GridTableBase
更新后手动刷新网格。下面是一个小的工作示例,希望能对您有所帮助

import wx, wx.grid

class GridData(wx.grid.PyGridTableBase):
    _cols = "a b c".split()
    _data = [
        "1 2 3".split(),
        "4 5 6".split(),
        "7 8 9".split()
    ]
    _highlighted = set()

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

    def GetNumberRows(self):
        return len(self._data)

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

    def GetValue(self, row, col):
        return self._data[row][col]

    def SetValue(self, row, col, val):
        self._data[row][col] = val

    def GetAttr(self, row, col, kind):
        attr = wx.grid.GridCellAttr()
        attr.SetBackgroundColour(wx.GREEN if row in self._highlighted else wx.WHITE)
        return attr

    def set_value(self, row, col, val):
        self._highlighted.add(row)
        self.SetValue(row, col, val)

class Test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)

        self.data = GridData()
        self.grid = wx.grid.Grid(self)
        self.grid.SetTable(self.data)

        btn = wx.Button(self, label="set a2 to x")
        btn.Bind(wx.EVT_BUTTON, self.OnTest)

        self.Sizer = wx.BoxSizer(wx.VERTICAL)
        self.Sizer.Add(self.grid, 1, wx.EXPAND)
        self.Sizer.Add(btn, 0, wx.EXPAND)

    def OnTest(self, event):
        self.data.set_value(1, 0, "x")
        self.grid.Refresh()


app = wx.PySimpleApp()
app.TopWindow = Test()
app.TopWindow.Show()
app.MainLoop()