wx 网格 SetCellBackGroundColor() 没有按预期工作
wx grid SetCellBackGroundColor() is not working as expected
我有以下程序创建一个 wx 网格,设置一个单元格的背景颜色,然后通过设置被单击单元格的背景颜色来处理左键单击事件:
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()
除 self.grid.SetCellBackgroundColor(row, col, wx.LIGHT_GREY)
语句外,一切都如我所料。当我设置网格时调用有效,所以我认为我是正确的。我得到打印语句,所以事件绑定正在工作。对于该方法,我还需要做什么才能在单击的单元格上放置背景颜色?
您只需刷新 window。
我添加了一个颜色切换器,因此它会在单击时在灰色和白色之间切换。
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()
clr = self.grid.GetCellBackgroundColour(row, col)
if clr != wx.LIGHT_GREY:
self.grid.SetCellBackgroundColour(row, col, wx.LIGHT_GREY)
else:
self.grid.SetCellBackgroundColour(row, col, wx.WHITE)
self.Refresh()
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
我有以下程序创建一个 wx 网格,设置一个单元格的背景颜色,然后通过设置被单击单元格的背景颜色来处理左键单击事件:
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()
除 self.grid.SetCellBackgroundColor(row, col, wx.LIGHT_GREY)
语句外,一切都如我所料。当我设置网格时调用有效,所以我认为我是正确的。我得到打印语句,所以事件绑定正在工作。对于该方法,我还需要做什么才能在单击的单元格上放置背景颜色?
您只需刷新 window。
我添加了一个颜色切换器,因此它会在单击时在灰色和白色之间切换。
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()
clr = self.grid.GetCellBackgroundColour(row, col)
if clr != wx.LIGHT_GREY:
self.grid.SetCellBackgroundColour(row, col, wx.LIGHT_GREY)
else:
self.grid.SetCellBackgroundColour(row, col, wx.WHITE)
self.Refresh()
app = wx.App()
frame = MyForm().Show()
app.MainLoop()