Different foreground colors for each line in wxPython wxTextCtrl

I have a multiline

wx.TextCtrl() 

which I set for forground and Background colors for writing strings. I need to write different lines with different colors,

 wx.TextCtrl.setForgroundcolor() 

Changes all previous line colors. Is there any way around this?

+6
source share
3 answers

There are several methods in wx.Python for getting colored text.

  • wx.TextCtrl with wx.TE_RICH , wx.TE_RICH2 styles
  • wx.stc.StyledTextCtrl
  • wx.richtext.RichTextCtrl
  • wx.HtmlWindow (insert color labels into text)
  • wx.ListCrtl

You can get examples of all of them in the wxPython demo.

For example, you can change the colors of the foreground and background in any part of 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 also easy to use for writing strings 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 pointed out in another answer, using wx.ListCrtl can be a very complicated method if you work with lines of text (instead of multi-line text).

+11
source

If you need to set the color for the line before inserting, you can change the basic properties:

 ctrlText.SetDefaultStyle(wx.TextAttr(wx.GREEN,wx.BLACK)) #where wx.GREEN - foreground, wx.BLACK - background for text ctrlText.SetBackgroundColour(wx.BLACK) 

Here is an example:

 #!/usr/bin/python # -*- coding: utf-8 -*- import wx class ColorTextForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, "Colored text") panel = wx.Panel(self, wx.ID_ANY) self.log = wx.TextCtrl(panel, wx.ID_ANY, size=(100,100), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL|wx.TE_RICH) self.log.SetDefaultStyle(wx.TextAttr(wx.GREEN, wx.BLACK)) #self.log.SetBackgroundColour(wx.BLACK) self.bgColor = wx.WHITE self.log.SetBackgroundColour(self.bgColor) btnRed = wx.Button(panel, wx.ID_ANY, 'Red') btnGreen = wx.Button(panel, wx.ID_ANY, 'Green') self.cbBG = wx.CheckBox(panel, label='White') self.Bind(wx.EVT_BUTTON, self.onButtonRed, btnRed) self.Bind(wx.EVT_BUTTON, self.onButtonGreen, btnGreen) self.Bind(wx.EVT_CHECKBOX, self.onCheckChangeBG, self.cbBG) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.log, 1, wx.ALL|wx.EXPAND, 5) sizer.Add(btnRed, 0, wx.ALL|wx.CENTER, 5) sizer.Add(btnGreen, 0, wx.ALL|wx.CENTER, 5) sizer.Add(self.cbBG, 0, wx.ALL|wx.CENTER, 5) panel.SetSizer(sizer) def onButtonGreen(self, event): self.log.SetDefaultStyle(wx.TextAttr(wx.GREEN, self.bgColor)) self.log.AppendText("Geen\n") def onButtonRed(self, event): self.log.SetDefaultStyle(wx.TextAttr(wx.RED,self.bgColor)) self.log.AppendText("Red\n") def onCheckChangeBG(self, e): sender = e.GetEventObject() isChecked = sender.GetValue() if isChecked: self.bgColor = wx.BLACK self.cbBG.SetLabel('Black') else: self.bgColor = wx.WHITE self.cbBG.SetLabel('White') if __name__ == "__main__": app = wx.PySimpleApp() frame = ColorTextForm().Show() app.MainLoop() 

Result:

enter image description here

+5
source

An alternative to wx.TextCtrl is wx.ListCtrl , which has an interface for each line.

You can set the colors of wx.ListCtrl using SetItemBackgroundColour as well as SetItemTextColour

0
source

Source: https://habr.com/ru/post/899084/


All Articles