如何使用 ScrolledText 小部件对文本进行多色处理? [英] How to multicolour text with ScrolledText widget?

查看:43
本文介绍了如何使用 ScrolledText 小部件对文本进行多色处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from tkinter import *
from tkinter.scrolledtext import ScrolledText

window= Tk()
window.geometry('970x45')
box = ScrolledText(window, width=70, height=7).pack()
box.insert(END, "Ehila") #this insert "Ehila" into the box
box.congif(foreground='green') #this change the colour of "Ehila" into green colour
box.insert(END, "Now") #this insert "Now" into the box
box.congif(foreground='red') #this change the colour of "Now" into red colour but also "Ehila" become red and I don't want this!

我想用不同的颜色为每个文本着色,但最终我没有得到这个结果.如何保持每次插入的颜色?

I would like to colour each text colored with a different colour but I don't obtain at the end this result. How can I keep the colour of each insertion?

推荐答案

插入带有标签的文本(insert 方法接受可选的标签参数).稍后使用 Text.tag_config 来更改标记文本的颜色.

Insert text with tags (insert method accept optional tag parameter(s)). Later use Text.tag_config to change the color of texts which tagged.

from tkinter import *
from tkinter.scrolledtext import ScrolledText

window = Tk()
window.geometry('970x45')
box = ScrolledText(window, width=70, height=7)
box.pack()
box.insert(END, "Ehila", 'name')  # <-- tagging `name`
box.insert(END, "Now", 'time')  # <-- tagging `time`
box.tag_config('name', foreground='green')  # <-- Change colors of texts tagged `name`
box.tag_config('time', foreground='red')  # <--  Change colors of texts tagged `time`

window.mainloop()

这篇关于如何使用 ScrolledText 小部件对文本进行多色处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆