【发布时间】: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 键并向下移动列表,同时将光标和屏幕移动到下一个匹配项。
也许我在这里遗漏了一些简单的东西,但我被困住了,不知道该如何处理。我花了很多时间在网上搜索答案,但我找不到任何可以做我想做的事情。我发现的所有线程都与突出显示所有单词有关,仅此而已。
【问题讨论】: