【问题标题】:Seach textbox for a word and move cursor to next match in the textbox?在文本框中搜索单词并将光标移动到文本框中的下一个匹配项?
【发布时间】:2017-10-25 02:09:05
【问题描述】:

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

我有两种方法可以在文本框中搜索单词。

一种方法是查找每个匹配项并更改正在搜索的单词的字体、颜色和大小,使其从文本的其余部分中脱颖而出。这是我使用的函数。

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

我尝试的另一种方法是突出显示正在搜索的单词的每个匹配项。这是它的功能。

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")',我注意到它只会将我移动到找到的第一个匹配项。我不知道该怎么做才能将光标移动到下一场比赛等等。

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

也许我在这里遗漏了一些简单的东西,但我被困住了,不知道该如何处理。我花了很多时间在网上搜索答案,但我找不到任何可以做我想做的事情。我发现的所有线程都与突出显示所有单词有关,仅此而已。

【问题讨论】:

    标签: python search tkinter


    【解决方案1】:

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

    例如,假设您的匹配项都具有“搜索”标签,您可以使用以下内容实现“转到下一个匹配项”功能:

    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"
    

    【讨论】:

    • 这很好用。我刚刚将此函数添加到我的代码中,并使用searchEntry.bind("&lt;Shift-Return&gt;" , next_match) 将我的输入字段绑定到此函数,稍后我应该可以将其添加到主搜索功能中,但现在我可以使用 Shift Enter 执行下一个任务。我确信我可以将 alt enter 绑定到相反的位置。谢谢。
    • @stackoverflow.com/users/7432/bryan-oakley 我有一个类似的程序,它使用跟踪来搜索进入输入框的字符。我的代码看起来像 Mike 的第一个示例。我想添加一个按钮来搜索下一个单词匹配。你能建议我使用第一个示例代码实现“next_match”函数的方法吗?
    猜你喜欢
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多