I am trying things with wxStaticText (a label). I can chnage background colour, but not the text colour:

import wx

class MyPanel(wx.Panel):
  def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id)
    str1 = "Camembert cheese is the best!"
    self.label1 = wx.StaticText(self, -1, str1 , wx.Point(15, 30))
    self.label2 = wx.StaticText(self, -1, str1 , wx.Point(15, 50))
    self.label2.SetBackgroundColour("Yellow")
    #self.label2.SetTextColour("blue")  # gives error!
    
    
app = wx.PySimpleApp()
myframe = wx.Frame(None, -1, "wxPython Labels", size = (330, 150))
MyPanel(myframe, -1)
myframe.Show(1)
app.MainLoop()

How to change text colour?

Dani AI

Generated

Short answer: is right — wx.StaticText does not implement SetTextColour, so calling that will raise an attribute error; change the label colour with the foreground/colour APIs instead. (docs.wxpython.org)

Colors can be supplied as a wx.Colour (RGB or hex), or by name; using a wx.Colour object avoids ambiguity when you need exact RGB values. After changing colours you may need to force a redraw with Refresh() (or Refresh() followed by Update() if you need the change immediately). (docs.wxpython.org)

Be aware of platform quirks: on some ports (notably GTK and macOS) wx.StaticText may be drawn by the parent rather than being a full native subwindow, so background colour changes can be ignored. When you need full control (clicks, non-inherited background, reliable recolouring) use the generic implementation wx.lib.stattext.GenStaticText, which is a real window and won’t be affected by those limitations. Also consider using SetOwnForegroundColour/SetOwnBackgroundColour or inspecting inherited attributes when themes seem to override your changes. (docs.wxpython.org)

Quick troubleshooting checklist:

  • Replace SetTextColour with the foreground API and pass a wx.Colour.
  • Call Refresh() on the control (or its parent) after changing colours.
  • If the background still won’t change, try GenStaticText.
    Minimal example of the redraw step:
label.SetForegroundColour(wx.Colour(0, 0, 255))
label.Refresh()

If a change still has no visible effect, check whether the parent window or a generated UI builder call is resetting colours (this can mask your label changes). (stackoverflow.com)

For labels you have to use SetForegroundColour ...

import wx

class MyPanel(wx.Panel):
  def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id)
    str1 = "Camembert cheese is the best!"
    self.label1 = wx.StaticText(self, -1, str1 , wx.Point(15, 30))
    self.label2 = wx.StaticText(self, -1, str1 , wx.Point(15, 50))
    self.label2.SetBackgroundColour("yellow")
    # use SetForegroundColour() and not SetTextColour()
    self.label2.SetForegroundColour("blue")
    
    
app = wx.PySimpleApp()
myframe = wx.Frame(None, -1, "wxPython Labels", size = (330, 150))
MyPanel(myframe, -1)
myframe.Show(1)
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.