如何设置wxPython网格的背景颜色?
我正在使用wxPython的网格控件,但我无法设置网格的背景颜色(也就是那些没有被单元格填充的部分)。我尝试使用grid.SetBackgroundColour这个方法,但没有成功;显示的背景颜色总是系统默认的颜色。
wx.version() -> 2.8.10.1 (msw-unicode)
sys.version -> 2.5 (r25:51908, 2006年9月19日, 09:52:17) [MSC v.1310 32位 (Intel)]
操作系统版本 -> Windows XP SP3,不过我也在一个基于Ubuntu的Python live cd上试过,结果也是一样。
import wx
import wx.grid
class TestFrame (wx.Frame):
def __init__ (self):
wx.Frame.__init__ (self, None, title="Grid Table", size=(640,480))
grid = wx.grid.Grid(self, size=(300,300))
grid.CreateGrid(2,2)
grid.SetCellValue(0,0,"1")
color = (100,100,255)
attr = self.cellAttr = wx.grid.GridCellAttr()
attr.SetBackgroundColour(color)
# for row, col in
for row in xrange(2):
for col in xrange(2):
grid.SetAttr(row, col, attr)
grid.SetBackgroundColour(color) # <<< This don't work!
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()
3 个回答
0
这是来自@Rolf of Saxony的一个有效代码片段。
链接到相关内容:wx grid SetCellBackGroundColor()的使用效果不如预期
这个片段是基于@interjay和@jake77上面的回答:
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_SELECT_CELL, 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.SetDefaultCellBackgroundColour(wx.Colour(wx.WHITE))
self.grid.SetLabelBackgroundColour(wx.Colour(wx.WHITE))
else:
self.grid.SetCellBackgroundColour(row, col, wx.GREEN)
self.Refresh()
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
而第二个代码片段只部分有效——表头和行标签显示得很好,但单元格却没有(如果你愿意,可以随意修复它!):
import wx
import wx.grid as gridlib
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Grid Color Bg")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.grid = gridlib.Grid(panel)
self.grid.CreateGrid(7, 5)
self.grid.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.colorBg)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.grid, 1, wx.EXPAND, 5)
panel.SetSizer(sizer)
# ----------------------------------------------------------------------
def colorBg(self, event):
""""""
clr = self.grid.GetDefaultCellBackgroundColour()
clr1 = self.grid.GetLabelBackgroundColour()
if clr != wx.Colour(wx.WHITE):
self.grid.SetDefaultCellBackgroundColour(
self.grid.SetLabelBackgroundColour(wx.Colour(wx.WHITE))
)
if clr1 != wx.Colour(wx.WHITE):
self.grid.SetDefaultCellBackgroundColour(wx.Colour(wx.WHITE))
# Run the program
if __name__ == "__main__":
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
1
使用 grid.SetDefaultCellBackgroundColour(grid.GetLabelBackgroundColour())
这个命令可以把所有单元格的背景颜色设置成一样的颜色。之后,你可以根据每个单元格的需要来重新绘制它们。
3
grid.SetDefaultCellBackgroundColour(color)
这个命令会把整个网格的背景颜色都改变,包括单元格外面的区域。