繁体   English   中英

Tkinter Text Widget颜色某些文本

[英]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_addtag_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM