The search method returns the index of the first match in or after the start index and, if necessary, the number of matches of characters. You are responsible for highlighting what was found using this information.
For example, consider this search:
countVar = tk.StringVar() pos = text.search("a red car", "1.0", stopindex="end", count=countVar)
If a match is found, pos will contain the index of the first match character, and countVar will contain the number of matching characters. You can use this information to highlight a match using an index of the form "index + N chars" or the abbreviation "index + Nc". For example, if pos were 2.6 and count was 9, the index of the last match would be 2.6+9c
After that, and provided that you have already set up a tag with the name "search" (for example, text.tag_configure("search", background="green") ), you can add this tag to the beginning and end of the match as follows:
text.tag_add("search", pos, "%s + %sc" (pos, countVar.get()))
To highlight all matches, just put the search command in a loop and adjust the starting position one character after the end of the previous match.
source share