[英]How i can highlight some words in a tkinter Text widget?
這是我的第一個問題...我正在做一個突出顯示列表中輸入的單詞的項目我如何突出顯示它? 我的代碼:
import tkinter
root = tkinter.Tk()
words = ["Potatos","Tomatoes","Carrots"]
box = tkinter.Text(root)
box.pack()
root.mainloop()
如果我輸入單詞列表中的單詞,我如何突出顯示顏色的文本? 對不起,如果我聽起來很愚蠢,但我是新手
嘗試這個:
highlight_text()
function。clear()
function。Button
以突出顯示。Button
以清除。List
修改后的代碼:
import tkinter
root = tkinter.Tk()
def highlight_text():
try:
box.tag_add("start", "sel.first", "sel.last")
except tk.TclError:
pass
def clear():
box.tag_remove("start", "1.0", 'end')
words = "Potatos","Tomatoes","Carrots"
box = tkinter.Text(root, width=25, height=5)
box.insert(tkinter.INSERT, words)
box.pack()
box.tag_configure("start", background="black", foreground="red")
highlight_btn = tkinter.Button(root, text="Highlight", command=highlight_text)
highlight_btn.pack(side=tkinter.LEFT)
clear_btn = tkinter.Button(root, text="Clear", command=clear)
clear_btn.pack(side=tkinter.LEFT)
root.mainloop()
Output之前,高亮清除:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.