I’m writing a small program using wxPython that displays a finite sized grid of floating point numbers which is stored in parallel as a numpy array. I want the colors of the cells to represent the value of the number in that cell, as a smooth gradient from red to blue where fully blue represents the minimum value in the grid and fully red is the maximum.
The problem I’m having is that when I call SetCellBackgroundColour the cell doesn’t always change, or doesn’t completely change. For instance, sometimes when I change the value of a cell, only part of the cell will change color or the whole thing will turn fully blue or fully red. Usually if I give it a second and click around in different cells it eventually figures its self out and looks correct.
Here is the event handler I have attached to wx.grid.EVT_GRID_CELL_CHANGE:
def onGridChange(self, evt):
row, col = evt.GetRow(), evt.GetCol()
value = float(self.myGrid.GetTable().GetValue(row, col))
self.table[row][col] = value
self.update_colors()
evt.Skip()
def update_colors(self):
table_min = self.table.min()
table_max = max(table_min + 1, self.table.max()) # to avoid dividing by zero later on.
table_range = table_max - table_min
for row in range(self.num_rows):
for col in range(self.num_cols):
percentage = (self.table[row][col]-table_min)/table_range
color = (int(255*percentage), 0, int(255*(1.-percentage)))
self.myGrid.SetCellBackgroundColour(row, col, color)
As pointed out by @jozzas I needed to run a
self.myGrid.ForceRefresh()on the grid whenever cells outside of the currently active cell were being updated. That fixed my problem! Thanks!