Try this:
- Add
highlight_text()
function.
- Add
clear()
function.
- Add
Button
for highlight.
- Add
Button
for clear.
- Remove brace bracket that you don't needed
List
Modified code:
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 before, highlight and clear:
