I'm having trouble highlighting alternative rows in a grid. Having created the grid and filled it with data, highlighting alternative rows works as expected. But when new data is loaded, I delete all rows, then add new rows as required, and this time grid highlighting raises the exception unhandled TypeError. This has me stumped- any suggestions? The code below produces the same error (click the button twice):-
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()
In your example, your
GridCellAttr
instance has gone out of scope and been deleted by the second time you press the button. I'm surprised it worked the first time, to be honest. When I click the button twice, I get the following error:Digging a bit deeper, if you look at the 3rd argument, you will see that it is the following:
Anyway, here's one simple approach that worked every time for me: