在文本框中搜索一个单词并将光标移动到文本框中的下一个匹配项? [英] Seach textbox for a word and move cursor to next match in the textbox?

查看:41
本文介绍了在文本框中搜索一个单词并将光标移动到文本框中的下一个匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个小部件,可以搜索我的主文本框并突出显示与我的搜索匹配的单词.我遇到的问题是找到一种方法将光标移动到找到的第一个匹配项,然后将光标移动到下一次按 Enter 键时找到的下一个匹配项.

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())))

在第二种方法中,我使用了方法 'root.text.see("insert")' 并且我注意到它只会将我移动到找到的第一个匹配项.我不知道该怎么做才能将光标移动到下一场比赛等等.

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.

我希望能够多次按下 Enter 键并在将光标和屏幕移动到下一个匹配项的同时向下移动列表.

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.

推荐答案

您可以使用文本小部件方法 tag_next_rangetag_prev_range 来获取下一个或上一个的索引带有给定标签的字符.然后您可以将插入光标移动到该位置.

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"

这篇关于在文本框中搜索一个单词并将光标移动到文本框中的下一个匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆