![](/img/trans.png)
[英]Change color of certain words in Tkinter Text widget based on position in list
[英]Tkinter Text Widget color certain text
我有一个不可编辑的Text
窗口小部件,其中可以使用“ Entry
窗口小部件添加文本。 我希望Text
小部件中的某些文本的颜色与其余Text
颜色不同,具体取决于发送的文本类型。例如,取决于类型的可能输出可能是:
*Line one text* (color: Black)
*Line two text* (color: Blue)
*Line three text* (color: Black)
从我发现的内容来看,似乎可以使用Text
小部件具有的tag_add
和tag_configure
方法来执行此操作,但我不确定如何执行此操作。
我有以下将文本追加到“ Text
小部件的方法,并具有更改文本颜色的能力:
def append_to_display(self, text, color=standard):
self.display.configure(state=NORMAL)
self.display.tag_configure("color", foreground=color)
self.display.insert(END, text + "\n", "color")
self.display.configure(state=DISABLED)
但是,如果我将颜色更改为“绿色”,它不仅会更改为已发送的文本,还会更改所有文本。
那么如何使它仅对发送的文本起作用?
另请注意,我正在运行Python 3.6.1
您需要为每种颜色使用唯一的标签名称。
def append_to_display(self, text, color=standard):
tag_name = "color-" + color
self.display.tag_configure(tag_name, foreground=color)
...
self.display.insert(END, text + "\n", tag_name)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.