Hopefully a simple question

I have a python 3.9 grid using wxPython 4.20

All I need to do is change the ColLabel color background to black.

I’ve tried a miriad of things that did nothing. And I can’t seem to find anything that actually does it.

Any help in changing just the ColLabelBackground to black would be appreciated

 import wx
 import wx.grid as gridlib

 class MyForm(wx.Frame):

    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="A Simple Grid")
        panel = wx.Panel(self)

        myGrid = gridlib.Grid(panel)
        myGrid.CreateGrid(12, 8)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(myGrid, 1, wx.EXPAND)
        panel.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    frame = MyForm().Show()
    app.MainLoop()
import wx
import wx.grid as gridlib
import wx.lib.mixins.gridlabelrenderer as glr

class CustomGrid(gridlib.Grid, glr.GridWithLabelRenderersMixin):
    def __init__(self, parent):
        gridlib.Grid.__init__(self, parent)
        glr.GridWithLabelRenderersMixin.__init__(self)


class CustomColLabelRenderer(glr.GridLabelRenderer):
    def __init__(self, text_color, background_color):
        super(CustomColLabelRenderer, self).__init__()
        self.text_color = text_color
        self.background_color = background_color

    def Draw(self, grid, dc, rect, col):
        dc.SetPen(wx.Pen(wx.WHITE))
        dc.SetBrush(wx.Brush(self.background_color))
        dc.DrawRectangle(rect)
        text = grid.GetColLabelValue(col)
        hAlign, vAlign = grid.GetColLabelAlignment()
        self.DrawText(grid, dc, rect, text, hAlign, vAlign)

    def DrawText(self, grid, dc, rect, text, hAlign, vAlign):
        dc.SetBackgroundMode(wx.TRANSPARENT)
        dc.SetTextForeground(self.text_color)
        dc.SetFont(grid.GetLabelFont())
        rect = wx.Rect(*rect)
        rect.Deflate(2,2)
        grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)


class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, parent=None, title="Custom Grid Column Labels", size=(600,200))
        panel = wx.Panel(self)

        self.grid = CustomGrid(panel)
        self.grid.CreateGrid(2, 4)

        col_render = CustomColLabelRenderer(wx.WHITE, wx.BLACK)
        self.grid.SetDefaultColLabelRenderer(col_render)

        # If required, create CustomRowLabelRenderer and/or
        # CustomCornerLabelRenderer classes for these...
        # self.grid.SetDefaultRowLabelRenderer(row_render)
        # self.grid.SetCornerLabelRenderer(corner_render)

        self.grid.SetColLabelValue(0, "COL1")
        self.grid.SetColSize(0, 100)
        self.grid.SetColLabelValue(1, "COL2")
        self.grid.SetColSize(1, 100)
        self.grid.SetColLabelValue(2, "COL3")
        self.grid.SetColSize(2, 100)
        self.grid.SetColLabelValue(3, "COL4")
        self.grid.SetColSize(3, 100)

        self.grid.SetRowLabelValue(0, "ROW1")
        self.grid.SetRowLabelValue(1, "ROW2")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.grid)
        panel.SetSizer(sizer)


if __name__ == "__main__":
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

Tested using Python 3.10.6 + wxPython 4.2.0 gtk3 (phoenix) wxWidgets 3.2.0 on Linux Mint 21.1

Thanks
Unfortunately 1) I was asked if it could be done in 1 - 2 lines
2) it kept erroring on OSx python 3.9.13 wxPython 4.2.0

So we decided to leave the labels as they were as this would take more time than it was worth for this project.

Rome is beautiful :innocent:

1 Like