[英]how I can change the background color and text color of a textbox done with Tkinter?
如何在 python 中更改使用 Tkinter 完成的文本框的背景顏色? 我使用了下面的代碼,但我不明白為什么它不起作用
from Tkinter import *
def onclick():
pass
root = Tk()
text = Text(root)
text.pack()
text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="black", foreground="green")
root.mainloop()
它確實有效。 如果插入文本:
text.insert(1.0, 'Hello World')
在調用tag_add
和tag_config
方法之前,該標簽將附加到插入的文本中。 但是,在當前調用它時,沒有插入標簽的索引,因此實際上沒有標簽。
如果您想在用戶輸入小部件時實時修改文本內容,您可以將文本小部件綁定到一個按鍵事件,該事件調用一個為文本小部件添加和配置標簽的函數:
from Tkinter import *
def track_change_to_text(event):
text.tag_add("here", "1.0", "1.4")
text.tag_config("here", background="black", foreground="green")
root = Tk()
text = Text(root)
text.pack()
text.bind('<KeyPress>', track_change_to_text)
root.mainloop()
在創建文本框本身時,使用bg
確定背景顏色:
text = Text(root, bg = 'black')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.