2

因此,我需要通过单击按钮在文本框中找到每个匹配的单词添加滚动。这个函数只找到第一个匹配的词。如何将更新后的索引提供给绑定到按钮的函数?

def find(idx='1.0'):
    text.tag_remove('found', idx, END)
    s = edit.get()
    if s:
        idx = text.search(s, idx, nocase=1,
                          stopindex=END)
        if not idx:
            return None
  
        foundidx = '%s+%dc' % (idx, len(s))

        text.tag_add('found', idx, foundidx)
        text.see(foundidx)

        text.tag_config('found', foreground='red')
        edit.focus_set()

    return foundidx
4

1 回答 1

1

要实现这一点,您需要一个Text小部件和一个通过小部件显示示例文本的根。我这样做了:

import tkinter as tk

root = tk.Tk()

text = tk.Text(root)
text.pack()
text.insert('1.0','''
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

text by: https://www.loremipsum.de/
''')

在此之后,我按照评论中链接的说明进行操作。并通过保存位置和索引来扩展它。

countVar = tk.StringVar()
idxVar = tk.StringVar()
posis = []
def search():
    try:
        if idxVar.get():
            new_idx = float(idxVar.get()+countVar.get())
            pos = text.search('sed', new_idx, 'end', count=countVar)
            text.tag_add('search', pos, '%s + %sc' % (pos, countVar.get()))
            text.tag_configure('search', background='green')
            idxVar.set(pos)
            posis.append(pos)
        else:
            pos = text.search('sed', '1.0', 'end', count=countVar)
            text.tag_add('search', pos, '%s + %sc' % (pos, countVar.get()))
            text.tag_configure('search', background='green')
            idxVar.set(pos)
            posis.append(pos)
    except:
        idxVar.set('1.0')

button = tk.Button(root,text='search',command=search)
button.pack()

至少我使用该Text.see方法跳转到我的 posis 列表中的点:

show = tk.IntVar()
show.set(0)

def see():
    if show.get() <= len(posis)-1:
        pos = show.get()
        text.see(posis[show.get()])
        show.set(pos+1)
    else:
        show.set(0)
        

button2= tk.Button(root,text='see', command=see)
button2.pack()

root.mainloop()

我知道这段代码可以改进很多,但它应该显示原理,我曾经将它编码为自我解释。但如果有未解决的问题,请告诉我,我会改进这个答案。

于 2020-12-06T07:46:14.110 回答