[英]How can I set some text color in a tkinter text widget
can someone tell me how can I change color to certain parts of a tkinter text widget.有人可以告诉我如何将颜色更改为 tkinter 文本小部件的某些部分。 This is a simple view of my code (it's a chat so it has to display who sent the message):
这是我的代码的一个简单视图(它是一个聊天,所以它必须显示谁发送了消息):
T = Text(window, bg="black", fg="white", font=("bold", 12))
def addMessage(text)
T.insert("me: ")
T.insert(text+"\n")
addMessage("hi")
how can I set the "me: " part to be blue and the text part to stay white?如何将“我:”部分设置为蓝色,将文本部分设置为白色?
Try this:尝试这个:
from tkinter import *
window = Tk()
T = Text(window, bg="black", fg="white", font=("bold", 12))
# Create a tag named "blue" and set the color of it to blue
T.tag_config("blue", foreground="blue")
T.grid()
def addMessage(text):
# apply the tag while inserting
T.insert(END, "me: ", "blue")
T.insert(END, text+"\n")
addMessage("hi")
window.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.