gpt4 book ai didi

python - 如何使用 ScrolledText 小部件为文本添加多色?

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:13 25 4
gpt4 key购买 nike

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!

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

最佳答案

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

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()

关于python - 如何使用 ScrolledText 小部件为文本添加多色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38159653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com