我想查找选定的关键字,并在标签文本中更改其字体颜色
在终端中打印的下面的代码
import colorama as color
text = 'This is a long strings. How many strings are there'
x = 'strings'
if x in text:
print(text.replace(x,"{}{}{}".format(color.Fore.RED, x, color.Fore.RESET)))
root.mainloop()
该代码在终端中运行良好。之后,我尝试将打印代码应用到标签中
from tkinter import *
root = Tk()
Label(root, text=text.replace(x,"{}{}{}".format(color.Fore.RED, x, color.Fore.RESET)))
应用后的输出类似于标签中的内容:
This is a long 口[31mstrings. How many 口[31mstrings are there
我环顾了一下解决方案,发现colorama只在终端上起作用。有没有更好的方法在GUI中更改字符串的字体颜色?谢谢大家!
我从这个链接here找到了解决方案
我把它应用到我的代码中,它就像它想要的那样工作
图为:
Output
感谢@j_4321和@Bryan Oakley
相关问题 更多 >
编程相关推荐