【发布时间】:2013-01-03 22:15:18
【问题描述】:
我正在使用 wxPython 编写一个小程序,它显示一个有限大小的浮点数网格,该网格并行存储为一个 numpy 数组。我希望单元格的颜色代表该单元格中数字的值,作为从红色到蓝色的平滑渐变,其中全蓝色代表网格中的最小值,全红色代表最大值。
我遇到的问题是,当我调用SetCellBackgroundColour 时,单元格并不总是改变,或者没有完全改变。例如,有时当我更改单元格的值时,只有部分单元格会改变颜色,或者整个单元格会变成完全蓝色或完全红色。通常,如果我再给它一秒钟并在不同的单元格中单击它,它最终会自动识别并看起来正确。
这是我附加到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)
【问题讨论】:
-
您是否尝试在您的
SetCellBackgroundColour通话后添加self.myGrid.ForceRefresh()?您也可以尝试刷新整个窗口。 -
成功了,谢谢!该问题仅在范围更改时发生,即当当前选定的单元格之外的单元格正在更改颜色时。现在我将其设置为
ForceRefresh,仅当新值超出之前的范围时。