python - How to highlight a specific word in tkinter? -
i need highlight specific word in text within tkinter frame. in order find word, put balise in html. in text "hello i'm in |house|" want highlight word "house".
my frame defined that:
class framecodage(frame): self.t2codage = text(self, height=20, width=50)
and insert text code: fenetre.fcodage.t2codage.insert(end, res)
, res being variable containing text.
i saw code on other post:
class customtext(tk.text): '''a text widget new method, highlight_pattern() example: text = customtext() text.tag_configure("red", foreground="#ff0000") text.highlight_pattern("this should red", "red") highlight_pattern method simplified python version of tcl code @ http://wiki.tcl.tk/3246 ''' def __init__(self, *args, **kwargs): tk.text.__init__(self, *args, **kwargs) def highlight_pattern(self, pattern, tag, start="1.0", end="end", regexp=false): '''apply given tag text matches given pattern if 'regexp' set true, pattern treated regular expression. ''' start = self.index(start) end = self.index(end) self.mark_set("matchstart", start) self.mark_set("matchend", start) self.mark_set("searchlimit", end) count = tk.intvar() while true: index = self.search(pattern, "matchend","searchlimit", count=count, regexp=regexp) if index == "": break self.mark_set("matchstart", index) self.mark_set("matchend", "%s+%sc" % (index, count.get())) self.tag_add(tag, "matchstart", "matchend")
but few things don't understand: how can apply function case? when did call function? what's pattern , tag in case? i'm beginner in tkinter don't hesitate explain me code, or another.
instead of this:
class framecodage(frame): self.t2codage = text(self, height=20, width=50)
... this:
class framecodage(frame): self.t2codage = customtext(self, height=20, width=50)
next, create "highlight" tag, , configure want:
self.t2codage.tag_configure("highlight", foreground="red")
finally, can call highlight_pattern
method if standard method:
self.t2codage.highlight_pattern(r"\|.*?\|", "red", regexp=true)
Comments
Post a Comment