I have the following program to illustrate my problem:
import wx
import wx.grid as gridlib
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Sample grid")
self.grid = gridlib.Grid(self)
self.grid.CreateGrid(5,4)
self.grid.SetCellSize(4,1,1,2)
self.grid.SetDefaultCellAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
self.grid.SetColLabelSize(0) # eliminates spreadsheet-style row & col headers
self.grid.SetRowLabelSize(0)
self.grid.SetCellBackgroundColour(4, 3, wx.LIGHT_GREY)
rowHeight = 50
colWidth = 50
for i in range(1,5):
self.grid.SetRowSize(i, rowHeight)
for i in range(0,4):
self.grid.SetColSize(i, colWidth)
self.grid.Bind(gridlib.EVT_GRID_CELL_LEFT_CLICK, self.GridLeftClick, self.grid)
def GridLeftClick(self, event):
col = event.GetCol()
row = event.GetRow()
print(f"Got col {col} and row {row}")
self.grid.SetCellBackgroundColour(row, col, wx.LIGHT_GREY)
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
Everything works as I expect except for the SetCellBackgroundColour in the GridLeftClick event handler. I think the call must be in the right format, since it works when I’m setting up the grid, and the event handler itself is getting called, because I get the print statement there. What else do I need to do to set the background color (or colour) on a left click event?