Вопрос

I have a function that returns a string every time I'm clicking a button. Every time the button is clicked I want the string to be added to my widget text. But I want the last string added to be in red (and the text added before in black).

I didn't find an easy way to make it. Any tips?

Это было полезно?

Решение

You should look through the Tags section here: http://effbot.org/tkinterbook/text.htm

You can set this up pretty easily by making a couple of tags (using the tag_config() method on a Text widget), let's call them highlight and unhighlight:

text.tag_config('highlight', foreground='red')
text.tag_config('unhighlight', foreground='black')

Then, in your Button callback, attach these tags to the text:

def callback():
    # unhighlight everything from the beginning to the end
    text.tag_add('unhighlight', 1.0, END)

    # insert new text and apply the highlight tag to it
    text.insert(END, 'hello world ', 'highlight')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top