Seach textbox for a word and move cursor to next match in the textbox?

不问归期 提交于 2021-01-28 05:34:43

问题


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 first match found and then subsequently moving the cursor to the next match found the next time I hit enter.

I have 2 ways I can search a word in my textbox.

One way is to look for every match and change the font,color,size of the word being searched so it stands out from the rest of the text. Here is the function I use for that.

def searchTextbox(event=None):
    root.text.tag_configure("search", background="green")
    root.text.tag_remove('found', '1.0', "end-1c")
    wordToSearch = searchEntry.get().lower()
    idx = '1.0'
    while idx:
        idx = root.text.search(wordToSearch, idx, nocase=1, stopindex="end-1c")
        if idx:
            lastidx = '%s+%dc' % (idx, len(wordToSearch))
            root.text.tag_add('found', idx, lastidx)
            idx = lastidx
    root.text.tag_config('found', font=("times", 16, "bold"), foreground ='orange')

The other way I have tried is to highlight every match for the word being searched. Here is the function for that.

def highlightTextbox(event=None):
    root.text.tag_delete("search")
    root.text.tag_configure("search", background="green")
    start="1.0"
    if len(searchEntry.get()) > 0:
        root.text.mark_set("insert", root.text.search(searchEntry.get(), start))
        root.text.see("insert")

        while True:
            pos = root.text.search(searchEntry.get(), start, END) 
            if pos == "": 
                break       
            start = pos + "+%dc" % len(searchEntry.get()) 
            root.text.tag_add("search", pos, "%s + %dc" % (pos,len(searchEntry.get())))

In the second method I have used the method 'root.text.see("insert")' and I have noticed it will move me only to the first match found. I am stuck as to what I should do in order to move the cursor to the next match and so on.

I want to be able to hit the Enter key many times and move down the list while moving the cursor and the screen to the next match.

Maybe I am missing something simple here but I am stuck and not sure how I should deal with this. I have spent a good amount of time searching the web for an answer but I could not find anything that would do what I am trying to do. All the threads I have found are related to highlighting all the words and that's it.


回答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 to end of 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, in case this was called
    # via a key binding
    return "break"


来源:https://stackoverflow.com/questions/44163321/seach-textbox-for-a-word-and-move-cursor-to-next-match-in-the-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!