What I couldn't figure out is how to dynamically tag ranges of text with the Tkinter text widget. The idea is that when the user selects a range of text, it dynamically creates a tag to modify the style. Here's my code:
#...code...
tag = text_field.tag_ranges(SEL)
text_field.tag_add('sizesel',tag[0],tag[1])
text_field.tag_config('sizesel',font = appFont)
This code is part of a callback function that is bound to a Combobox
, so that the size of the text changes every time the value changes.
This code works great, but if I try to stylize a second line of text, it takes the style of the 1st开发者_如何学Go line.
If you want a unique style for each range you'll need to use a unique tag, because style information belongs to the tag rather than the range of text. The easiest method is to keep a global (or instance attribute) counter that you increment each time you add a tag, and use that as part of the tag name.
Here's how I did it:
tag = text_field.tag_ranges(SEL)
i = 0
for i in tag:
text_field.tag_add(i,tag[0],tag[1])
text_field.tag_config(i,font = appFont)
as you can see I added a simple for statement that goes on tag which is the variable that contains the indexes for the SEL tag.
精彩评论