Search code examples
pythonradio-buttonwxpython

Changing Font Color of wxpython Radio Button


I am using wxpython version 2.9.4.0 and python 2.7.9.

I am trying the change the color of the text for a radio button. I initialized by:

button = wx.RadioButton(panel, -1, 'Line', (200, 300))

I was able to change the color around the radio button by:

button.SetBackgroundColour((150, 150, 150))

But this is not the behavior I want. I want to change the color of the text, not the area around it. I expected that changing the foreground colour would change the text color of the radio button, as that is how the color is changed for static text (as shown here Change the colour of a StaticText, wxPython). The code I used for this is:

button.SetForegroundColour((0, 255, 0))

However, for reasons unknown to me, this did not change anything about the radio button. Am I mistaken that this command should change the text color of the radio button, and if so, what is the proper command?

Thanks in advance!


Solution

  • The SetForegroundColoour and SetBackgroundColour methods are not guaranteed to work. The reason is that wxPython uses the native widgets for the OS it is running on. If the native widget does not support changing the text's color, then these methods will have no effect. Some widgets allow changes to color on Mac while the same widgets on Windows do not.

    If you really need to change text color in a radio button, then you'll probably need to create a custom widget. See the following: