The Python Oracle

Explain Tkinter text search method

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle

--

Chapters
00:00 Question
00:56 Accepted answer (Score 15)
02:16 Answer 2 (Score 2)
02:50 Answer 3 (Score 0)
03:16 Thank you

--

Full question
https://stackoverflow.com/questions/1946...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #search #tkinter #tkintertext

#avk47



ACCEPTED ANSWER

Score 15


The search method returns the index of the first match at or after the starting index, and optionally the number of characters that matched. You are responsible for highlighting what it found by 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 character of the match and countVar will contain the number of characters that matched. You can use this information to highlight the match by using an index of the form "index + N chars" or the shorthand "index + Nc". For example, if pos was 2.6 and count was 9, the index of the last character of the match would be 2.6+9c

With that, and assuming you've already configured a tag named "search" (eg: text.tag_configure("search", background="green")), you can add this tag to the start and end of the match like this:

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 to be one character past the end of the previous match.




ANSWER 2

Score 2


It may be a problem of the indexes.

In a program of mine, i have to search the start index and calculate the end index

my method for example, it works fine:

def highlight(self):
    start = 1.0
    pos = self.area_example.search(self.item.name, start, stopindex=END)
    while pos:
        length = len(self.item.name)
        row, col = pos.split('.')
        end = int(col) + length
        end = row + '.' + str(end)
        self.area_example.tag_add('highlight', pos, end)
        start = end
        pos = self.area_example.search(self.item.name, start, stopindex=END)
    self.area_example.tag_config('highlight', background='white', foreground='red')



ANSWER 3

Score 0


I have looked into @Bryan Oakley's approach and added some lines of code, in case you would like to highlight all the matches :

    def search_for_me(event):
    start_pos = "1.0"
    for tag in text.tag_names():
        text.tag_remove(tag, "1.0", "end")
    countVar = StringVar()
    while start_pos != "end":
        pos = text.search(search_entry.get(), start_pos, stopindex="end", 
        count=countVar)
        start_pos =  "%s + %sc" % (pos, int(countVar.get())+1)
        text.tag_configure("search", background="yellow")
        text.tag_add("search", pos, "%s + %sc" % (pos, countVar.get()))