wxPython wxTextCtrl 每行的前景色不同 [英] different foreground colors for each line in wxPython wxTextCtrl
问题描述
我有一个多行
wx.TextCtrl()
我设置的对象是用于书写字符串的前景色和背景色.我需要用不同的颜色写不同的线条,
object which I set it's forground and Background colors for writing strings.I need to write different lines with different colors ,
wx.TextCtrl.setForgroundcolor()
也改变了所有以前的线条颜色.有没有办法解决这个问题?
changes all previous lines colors as well.Is there a way around this?
推荐答案
wx.Python 中有几种方法可以获取彩色文本.
There are several methods in wx.Python to get colored text.
wx.TextCtrl
withwx.TE_RICH
,wx.TE_RICH2
样式wx.stc.StyledTextCtrl
wx.richtext.RichTextCtrl
wx.HtmlWindow
(在文本中插入颜色标签)wx.ListCrtl
wx.TextCtrl
withwx.TE_RICH
,wx.TE_RICH2
styleswx.stc.StyledTextCtrl
wx.richtext.RichTextCtrl
wx.HtmlWindow
(inserting color tags in your text)wx.ListCrtl
您可以在 wxPython 演示中获得所有示例
You can get examples of all of them in the wxPython demo
例如,您可以在 wx.TextCrtl
的任何部分更改前景色和背景色:
For example, you can change fore and background colors in any part of a wx.TextCrtl
:
rt = wx.TextCtrl(self, -1,"My Text....",size=(200, 100),style=wx.TE_MULTILINE|wx.TE_RICH2)
rt.SetInsertionPoint(0)
rt.SetStyle(2, 5, wx.TextAttr("red", "blue"))
wx.richtext
也很容易用来写不同颜色的线条:
wx.richtext
is also easy to use to write lines with different colors:
rtc = wx.richtext.RichTextCtrl(self, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER)
rtc.BeginTextColour((255, 0, 0))
rtc.WriteText("this color is red")
rtc.EndTextColour()
rtc.Newline()
如其他答案所示,如果您处理文本行(而不是多行文本),使用 wx.ListCrtl
可能是一种非常直接的方法.
As indicated in other answer the use of a wx.ListCrtl
can be a very straighforward method if you work with lines of text (instead of multiline text).
这篇关于wxPython wxTextCtrl 每行的前景色不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!