开发者

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 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 widge开发者_JAVA技巧t I could use?


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜