Highlight a row in wxGrid (with wxPython) when a cell in that row changes programatically
How do I highlight a row after an update of the underlying PyGridTableBase? I can't seem to get my he开发者_如何学Goad around it.
I am able to create alternate row highlighting lines when the table is first drawn:
### 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
but not when an even fires.
Any help much appreciated.
You are doing the right thing, the only problem that comes to mind is that you perhaps didn't manually refresh the grid after the GridTableBase
update. Here is a small working example that will hopefully help you out.
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()
精彩评论