Skip to content Skip to sidebar Skip to footer

How To Colour Text In Statictext (wxpython) In Different Colours?

Is it somehow possible to have text of a StaticText field in wxpython coloured in different colours? I know you can change the colour of the whole text (value) of a StaticText fiel

Solution 1:

The wx.StaticText widget doesn't support that sort of thing. You'd want to use one of the StyledText controls instead. the RichText control would probably work too.

Solution 2:

This should help.

text.SetForegroundColour((255,0,0)) # set text color
text.SetBackgroundColour((0,0,255)) # set text back color

*the colors are in RGB format. You can find a list of them here. Replace the numbers I have put in with whatever color you want.

Solution 3:

Maybe a little late to the party but maybe it helps someone.

Depending on what platform you are under you could use the method SetLabelMarkup which allow using HTML-like tags. It doesn't work on Windows but on some Linux versions.

For example, if you want a bi-colored text you can write

text.SetLabelMarkup('<spanforeground=\'red\'>Hello, </span><spanforeground=\'blue\'>World</span>

The documentation can be found here

Picture: Result on Raspbian

Post a Comment for "How To Colour Text In Statictext (wxpython) In Different Colours?"