wx.DC foreground color

Hi,

I’m working with wx.grid.Grid and custom column headers using wx.lib.mixins.gridlabelrenderer.GridLabelRenderer. I am trying to create a black background with white foreground text column header.

Unfortunately, while the background color works fine, the foreground color is always black, not the selected color.

The code example is based off of the demo for GridLabelRenderer with minor mods.

import wx
import wx.grid as gridLib
import wx.lib.mixins.gridlabelrenderer as glr

class labelGridRenderer(glr.GridLabelRenderer):
def init(self):
self._bgcolor = “#000000
self._fgcolor = wx.Colour(255, 255, 255)

def Draw(self, grid, dc, rect, col):
    dc.SetBrush(wx.Brush(self._bgcolor))
    dc.SetPen(wx.TRANSPARENT_PEN)
    dc.DrawRectangle(rect)
    hAlign, vAlign = grid.GetColLabelAlignment()
    text = grid.GetColLabelValue(col)
    self.DrawBorder(grid, dc, rect)
    dc.SetTextForeground(self._fgcolor)
    self.DrawText(grid, dc, rect, text, hAlign, vAlign)

What am I missing?

The DrawText method does this:

    dc.SetTextForeground(grid.GetLabelTextColour())

https://github.com/wxWidgets/Phoenix/blob/master/wx/lib/mixins/gridlabelrenderer.py#L220

So it is undoing the color you set it to. You can either do a grid.SetLabelTextColour or draw the text on the dc yourself.

···

On Monday, July 1, 2019 at 7:23:32 PM UTC-7, J Grimmtooth wrote:

Hi,

I’m working with wx.grid.Grid and custom column headers using wx.lib.mixins.gridlabelrenderer.GridLabelRenderer. I am trying to create a black background with white foreground text column header.

Unfortunately, while the background color works fine, the foreground color is always black, not the selected color.

    self.DrawText(grid, dc, rect, text, hAlign, vAlign)

What am I missing?

Robin

Robin,

Okay, I did it like this, and it worked, thanks :slight_smile:

See any obvious gotchas before I move on to the next problem?

def Draw(self, grid, dc, rect, col):
    dc.SetBrush(wx.Brush(self._bgcolor))
    dc.SetPen(wx.TRANSPARENT_PEN)
    dc.DrawRectangle(rect)
    hAlign, vAlign = grid.GetColLabelAlignment()
    text = grid.GetColLabelValue(col)
    self.DrawBorder(grid, dc, rect)
    grid.SetLabelTextColour(self._fgcolor)
    self.DrawText(grid, dc, rect, text, hAlign, vAlign)


Virus-free. www.avast.com

···

Regards,

Jeff

LGTM

···

On Tuesday, July 2, 2019 at 4:49:44 AM UTC-7, Jeff Grimmett wrote:

Robin,

Okay, I did it like this, and it worked, thanks :slight_smile:

See any obvious gotchas before I move on to the next problem?

Robin