How to colour text in StaticText (wxpython) in different colours?

一笑奈何 提交于 2019-12-13 00:38:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!