wx.DC foreground color

781 views
Skip to first unread message

J Grimmtooth

unread,
Jul 1, 2019, 10:23:32 PM7/1/19
to wxPython-users
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?

Robin Dunn

unread,
Jul 1, 2019, 11:00:26 PM7/1/19
to wxPython-users


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?


The DrawText method does this:

        dc.SetTextForeground(grid.GetLabelTextColour())
 

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.

--
Robin

Jeff Grimmett

unread,
Jul 2, 2019, 7:49:44 AM7/2/19
to wxpytho...@googlegroups.com
Robin,

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

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)

Regards,

Jeff


Virus-free. www.avast.com

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/d7dce1ca-02b0-4365-bbb6-590cabd88f0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robin Dunn

unread,
Jul 3, 2019, 1:51:06 PM7/3/19
to wxPython-users
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 :)

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

LGTM

--
Robin 
Reply all
Reply to author
Forward
0 new messages