1

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

絢瀬絵里
  • 1,013
  • 2
  • 14
  • 27

3 Answers3

1

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.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
0

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)
JFL
  • 1,502
  • 14
  • 18
0

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