如何使用滚动文本控件多色文本?

2024-10-01 15:48:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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!

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


Tags: thefromimportboxtkinterredwindowthis
1条回答
网友
1楼 · 发布于 2024-10-01 15:48:30

插入带标记的文本(insert方法接受可选标记参数)。稍后使用^{}更改标记的文本的颜色。在

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

相关问题 更多 >

    热门问题