I have a multiline
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?
There are several methods in wx.Python to get colored text.
wx.TextCtrl
with wx.TE_RICH
, wx.TE_RICH2
styles
wx.stc.StyledTextCtrl
wx.richtext.RichTextCtrl
wx.HtmlWindow
(inserting color tags in your text)
wx.ListCrtl
You can get examples of all of them in the wxPython demo
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
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()
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).