i wrote small function to highlight search pattern in text widget and assigned it to a button "Find". But turned out that it doesn't highlight needed pattern in text widget and just stucks.
def find():
xml.tag_delete("search")
xml.tag_configure("search", background="green")
while True:
index = xml.search(fi.get(), "1.0", END)
if index == "":
break
start = index + "+%dc" % len(fi.get())
xml.tag_add("search", index, "%s + %dc" % (index,len(fi.get())))
Who can tell me what am i doing wrong? xml
is a text widget, fi
is a entry widget, so a pattern is usually what user puts in fi
widget.
Any help would be appreciated. Thanks
Every time you search, you search from "1.0" to the end of the document. If what you're searching for is in the document even once, this while loop will never end because index
will never be an empty string.
The solution is to do start="1.0"
before the loop, and then modify your search to start at start
since you're updating this variable at the end of your loop.
start = "1.0"
while True:
index = xml.search(fi.get(), start, END)