更改 Tk 标签小部件中单个单词的颜色 [英] Change color of single word in Tk label widget
问题描述
我想更改 Tkinter 标签小部件中单个单词的字体颜色.
I would like to change the font color of a single word in a Tkinter label widget.
我知道可以使用 Text 小部件实现类似于我想要做的事情.例如,将YELLOW"一词显示为黄色:
I understand that something similar to what I would like to be done can be achieved with a Text widget.. for example making the word "YELLOW" show in yellow:
self.text.tag_config("tag_yel", fg=clr_yellow)
self.text.highligh_pattern("YELLOW", "tag_yel")
但是我的文本是静态的,我想要的只是将YELLOW"一词更改为黄色字体,将RED"更改为红色字体,而且我似乎无法弄清楚如何更改文本颜色而不使用 <代码>label.config(fg=clr).
But my text is static and all I want is to change the word "YELLOW" to show as yellow font and "RED" in red font and I cannot seem to figure out how to change text color without changing it all with label.config(fg=clr)
.
任何帮助将不胜感激
推荐答案
你不能为所欲为.一个标签只支持一种前景色和一种背景色.解决方案是使用文本或画布小部件,或使用两个单独的标签.
You cannot do what you want. A label supports only a single foreground color and a single background color. The solution is to use a text or canvas widget., or to use two separate labels.
这篇关于更改 Tk 标签小部件中单个单词的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!