我将如何添加在文本小部件中搜索文本的搜索功能?
* 根据用户输入进行搜索
def openFile():
global text
artiststxt = tkinter.Tk()
artiststxt.title('Artists')
artiststxt.geometry('300x360')
artiststxt.minsize(300,360)
artiststxt.maxsize(500,360)
file = open('Artists.txt','r', encoding='utf-8')
lines = file.read()
scrollbar = Scrollbar(artiststxt, jump = 1)
text = Text(artiststxt, yscrollcommand = scrollbar.set)
scrollbar.configure(command=text.yview)
text.insert(INSERT, lines)
text.config(font=('Fixedsys', 15), fg = 'darkblue', bg = 'lightgray')
menu = tkinter.Menu(artiststxt,tearoff=0)
menu.add_command(label='Save', command = saveFile)
artiststxt.config(menu=menu)
scrollbar.pack(side=RIGHT, fill=BOTH)
text.pack()
编辑:
好的,我知道如何用这个搜索文本:
def get(event):
global searchent
text.tag_remove('found', '1.0', END)
s = searchent.get()
if s:
idx = '1.0'
while 1:
idx = text.search(s, idx, nocase=1, stopindex=END)
if not idx: break
lastidx = '%s+%dc' % (idx, len(s))
text.tag_add('found', idx, lastidx)
idx = lastidx
text.tag_config('found', foreground='red')
searchent.focus_set()
现在,假设搜索到的文本位于更下方。如何使滚动条向下滚动到搜索到的文本?