different foreground colors for each line in wxPython wxTextCtrl

2024/10/8 6:19:30

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?

Answer

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).

https://en.xdnf.cn/q/70154.html

Related Q&A

Access deprecated attribute validation_data in tf.keras.callbacks.Callback

I decided to switch from keras to tf.keras (as recommended here). Therefore I installed tf.__version__=2.0.0 and tf.keras.__version__=2.2.4-tf. In an older version of my code (using some older Tensorfl…

How to unpickle a file that has been hosted in a web URL in python

The normal way to pickle and unpickle an object is as follows:Pickle an object:import cloudpickle as cpcp.dump(objects, open("picklefile.pkl", wb))UnPickle an object: (load the pickled file):…

Control tick-labels from multi-level FactorRange

Ive got a three-level bokeh.models.FactorRange which I use to draw tick labels on a vbar-plot. The problem is that there are dozens of factors in total and the lowest-level labels get very cramped.I ca…

PyTorch torch_sparse installation without CUDA

I am new in PyTorch and I have faced one issue, namely I cannot get my torch_sparse module properly installed. In general, I wanted to use module torch_geometric - this I have installed. However, when …

Escaping XPath literal with Python

Im writing a common library to setup an automation test suite with Selenium 2.0 Pythons webdriver.def verify_error_message_present(self, message):try:self.driver.find_element_by_xpath("//span[@cla…

How to return two values in cython cdef without gil (nogil)

I have a function and I am trying to return a number and a vector of ints. What I have is cdef func() nogil:cdef vector[int] vectcdef int a_number...return a_number, vectbut this will give errors like …

Alias for a chain of commands

I have a tool with commands: step1, step2 and step3.I can chain them by calling:$ tool step1 step2 step3I would like to have an alias named all to run all the steps by calling:$ tool allI have found a …

Generate misspelled words (typos)

I have implemented a fuzzy matching algorithm and I would like to evaluate its recall using some sample queries with test data. Lets say I have a document containing the text:{"text": "T…

Get the inverse function of a polyfit in numpy

I have fit a second order polynomial to a number of x/y points in the following way:poly = np.polyfit(x, y, 2)How can I invert this function in python, to get the two x-values corresponding to a speci…

Installing an old version of scikit-learn

Problem StatmentIm trying to run some old python code that requires scikit-learn 18.0 but the current version I have installed is 0.22 and so Im getting a warning/invalid data when I run the code.What …