Start New Discussion within our Software Development Community

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?

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()
This question has already been answered. Start a new discussion instead.