如何使用 ScrolledText 小部件对文本进行多色处理?

编程入门 行业动态 更新时间:2024-10-28 01:13:47
本文介绍了如何使用 ScrolledText 小部件对文本进行多色处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..
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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:55:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390855.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   部件   多色   文本   ScrolledText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!