[英]How can I set some text color in a tkinter text widget
有人可以告訴我如何將顏色更改為 tkinter 文本小部件的某些部分。 這是我的代碼的一個簡單視圖(它是一個聊天,所以它必須顯示誰發送了消息):
T = Text(window, bg="black", fg="white", font=("bold", 12))
def addMessage(text)
T.insert("me: ")
T.insert(text+"\n")
addMessage("hi")
如何將“我:”部分設置為藍色,將文本部分設置為白色?
嘗試這個:
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.