Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 搜索文本框中的单词并将光标移动到文本框中的下一个匹配项?_Python_Search_Tkinter - Fatal编程技术网

Python 搜索文本框中的单词并将光标移动到文本框中的下一个匹配项?

Python 搜索文本框中的单词并将光标移动到文本框中的下一个匹配项?,python,search,tkinter,Python,Search,Tkinter,我目前有一个小部件,它将搜索我的主文本框并突出显示与我的搜索匹配的单词。我遇到的问题是找到一种方法将光标移动到找到的第一个匹配项,然后在下一次按enter键时将光标移动到找到的下一个匹配项 我有两种方法可以在文本框中搜索单词 一种方法是查找每一个匹配项,并更改正在搜索的单词的字体、颜色和大小,使其从文本的其余部分中脱颖而出。这是我用于此的函数 def searchTextbox(event=None): root.text.tag_configure("search", backgrou

我目前有一个小部件,它将搜索我的主文本框并突出显示与我的搜索匹配的单词。我遇到的问题是找到一种方法将光标移动到找到的第一个匹配项,然后在下一次按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())))
我尝试过的另一种方法是突出显示所搜索单词的每个匹配项。下面是用于此的函数

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键,并在将光标和屏幕移动到下一个匹配时向下移动列表


也许我错过了一些简单的事情,但我被困住了,不知道该怎么处理。我花了大量的时间在网上搜索答案,但我找不到任何可以做我想做的事情。我找到的所有线程都与突出显示所有单词相关,仅此而已。

您可以使用文本小部件方法
tag\u next\u range
tag\u prev\u 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"
def next_match(事件=无):
#将光标移动到当前匹配的末尾

while(root.text.compare(“insert”),“这很好用。我刚刚把这个函数添加到我的代码中,并用
searchEntry.bind(“,next_match)”将我的输入字段绑定到这个函数中)
我应该可以在以后将其添加到主搜索功能中,但现在我可以使用Shift-Enter执行下一个任务。我确信我可以将alt-Enter绑定到相反的位置。谢谢。@我有一个类似的程序,它使用跟踪搜索输入框中的字符。我的代码看起来像Mike的第一个示例。我想添加一个butt在上面,我将搜索下一个单词匹配。你能建议我如何使用第一个示例代码实现“next_match”函数吗?