Skip to content Skip to sidebar Skip to footer

Seach Textbox For A Word And Move Cursor To Next Match In The Textbox?

I currently have a widget that will search my main textBox and highlight the words that match my search. The problem I am running into is finding a way to move the cursor to the fi

Solution 1:

You can use the text widget methods tag_next_range and tag_prev_range to get the index of the next or previous character with the given tag. You can then move the insert cursor to that position.

For example, assuming that your matches all have the tag "search", you can implement a "go to next match" function with something like this:

def next_match(event=None):

    # move cursor toendof current match
    while (root.text.compare("insert", "<", "end") and"search"in root.text.tag_names("insert")):
        root.text.mark_set("insert", "insert+1c")

    # find next character with the tag
    next_match = root.text.tag_nextrange("search", "insert")
    if next_match:
        root.text.mark_set("insert", next_match[0])
        root.text.see("insert")

    # prevent default behavior, incase this was called
    # via a key binding
    return"break"

Post a Comment for "Seach Textbox For A Word And Move Cursor To Next Match In The Textbox?"