Reputation: 13
I have a text widget with some text. Within the text from characters 424 to 478 I would like to change its color using tag_add().
The problem is that tag_add() requires positioning in "6.15" format, meaning sixth line fifteenth character. However I don't know how many new lines precede the 424th character nor what is the remainder to calculate the exact column. Is there a method to convert from an absolute byte offset to line/column index?
Upvotes: 0
Views: 488
Reputation: 385900
The text widget supports a limited expression syntax with indexes. Among other things you can add and subtract characters from an index. For example, you can use "1.0 + 100 chars"
(or "1.0+100c"
) to mean "line one, character zero, plus 100 characters".
The official python documentation punts on documenting this, choosing to refer you to the offical tcl/tk documentation here: http://tcl.tk/man/tcl8.5/TkCmd/text.htm#M16
This is also documented on the effbot site here: http://effbot.org/tkinterbook/text.htm (see the section "Expressions")
Upvotes: 2
Reputation: 5289
If you are using insert()
to add the text, you can specify the tag to use during the insert:
textw.tag_configure('red', background='red')
textw.insert(END, 'Some sample text ', (), 'with a bit of red', 'red', ' in the middle.')
Results in:
Also, depending on how your text is configured you can use:
textw.tag_add('red', 1.424, 1.478)
Upvotes: 0