问题
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 field like this:
text = wx.StaticText(panel, -1, 'random text')
text.SetForegroundColour('blue')
But how can I have the a text coloured in different colours, for example 'random' coloured in red and 'text' coloured in blue. Is there a way? Or is there some kind of other widget I could use?
回答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.
回答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.
回答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('<span foreground=\'red\'>Hello, </span><span foreground=\'blue\'>World</span>
The documentation can be found here
Picture: Result on Raspbian
来源:https://stackoverflow.com/questions/4420088/how-to-colour-text-in-statictext-wxpython-in-different-colours