python - Tkinter: Highlight/Colour specific lines of text based on a keyword -
i looking simple way search through line of text , highlight line if contains specific word. have tkinter text box has lots of lines like:
"blah blah blah failed blah blah"
"blah blah blah passed blah blah"
and set background colour of "failed" line red. far have:
for line in results_text: if "failed" in line: txt.tag_config("failed", bg="red") txt.insert(0.0,line) else: txt.insert(0.0,line)
this prints out want nothing colours
this wrong way change text colour. please help!!
use text.search.
from tkinter import * root = tk() t = text(root) t.pack() t.insert(end, '''\ blah blah blah failed blah blah blah blah blah passed blah blah blah blah blah failed blah blah blah blah blah failed blah blah ''') t.tag_config('failed', background='red') t.tag_config('passed', background='blue') def search(text_widget, keyword, tag): pos = '1.0' while true: idx = text_widget.search(keyword, pos, end) if not idx: break pos = '{}+{}c'.format(idx, len(keyword)) text_widget.tag_add(tag, idx, pos) search(t, 'failed', 'failed') search(t, 'passed', 'passed') #t.tag_delete('failed') #t.tag_delete('passed') root.mainloop()
Comments
Post a Comment