wxPython StyledTextCtrl Set (Partial) Text Color

1.2k Views Asked by At

How to set text color in a StyledTextCtrl, but only some word? I mean let's say I have

Lucy is Blue

and I want only the word 'Blue' to be colored Blue

3

There are 3 best solutions below

3
On

See the wxPython demo for the StyledTextCtrl. It shows how to do this exact thing. I think the bit you're looking for is something like this:

ed.StartStyling(190, 0xff)
ed.SetStyling(20, 2)

Where 190 is the 190th character and you set the style for the next 20 characters.

0
On

Use this where text_area is a StyledCtrlText

self.text_area.StyleSetSpec(stc.STC_P_DEFAULT,"fore:#FF0000")

next put the text you want to change color

0
On

To change the style of a line, you have to get the position of the first byte and the ending byte. then, you can define a Style (StyleSetSpec) that you make start at the first byte (StartStyling) and that you apply on the whole line (SetStyling). You have to re-apply the default style (0) at the ending byte. Here is my code:

# Move to line
self.editname.GotoLine(line-1)
# Get position
pos = self.editname.GetCurrentPos()
# Define style 4
self.editname.StyleSetSpec(4, "back:#ff0000")
# Starts style at position pos
self.editname.StartStyling(pos, 0xffff)
# Until posend position, apply style 4
self.editname.SetStyling(posend-pos, 4)
# Restore style 0 after the ending byte of the line
self.editname.SetStyling(posend, 0)