在文本小部件中搜索单词/字母(tkinter)

时间:2014-02-02 04:47:54

标签: python tkinter

如何添加搜索文本小部件中的文本的搜索功能? *从用户输入搜索

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

现在,让我们说搜索到的文字进一步下降。如何使滚动条向下移动到搜索到的文本?

2 个答案:

答案 0 :(得分:1)

好的,我明白了。花了一些时间,但非常值得。

首先我们在窗口中创建一个输入框,然后用enter键绑定它并放入一个.get事件

searchent.bind("<Return>", get)

按下回车键后,我们转到def get(event):

def get(event):
global searchent 
text.tag_remove('found', '1.0', END)
s = searchent.get() # Grabs the text from the entry box
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.see(idx)  # Once found, the scrollbar automatically scrolls to the text
    text.tag_config('found', foreground='red')
searchent.focus_set()

答案 1 :(得分:0)

我使用你的帖子Brian Fuller编写这段代码,我希望它能帮助你,因为它帮助了我,以及其他人......

from tkinter import *
from tkinter import messagebox as MessageBox

search_list = list()
s = ""

def reset_list():
    if s != entry_widget_name.get():
        print(entry_widget_name.get())
        search_list.clear()
        text_widget_name.tag_remove(SEL, 1.0,"end-1c")

def search_words():
    reset_list()
    global search_list
    global s
    text_widget_name.focus_set()
    s = entry_widget_name.get()

    if s:
        if search_list == []:
            idx = "1.0"
        else:
            idx = search_list[-1]

        idx = text_widget_name.search(s, idx, nocase=1, stopindex=END)
        lastidx = '%s+%dc' % (idx, len(s))

        try:
            text_widget_name.tag_remove(SEL, 1.0,lastidx)
        except:
            pass

        try:
            text_widget_name.tag_add(SEL, idx, lastidx)
            counter_list = []
            counter_list = str(idx).split('.')      
            text_widget_name.mark_set("insert", "%d.%d" % (float(int(counter_list[0])), float(int(counter_list[1]))))
            text_widget_name.see(float(int(counter_list[0])))
            search_list.append(lastidx)
        except:
            MessageBox.showinfo("Search complete","No further matches")
            search_list.clear()
            text_widget_name.tag_remove(SEL, 1.0,"end-1c")

root = Tk()
root.geometry("540x460")

lbl_frame_entry = LabelFrame(root, text="Enter the text to search", padx=5, pady=5)
lbl_frame_entry.pack(padx=10, pady=5, fill="both")

entry_widget_name = Entry(lbl_frame_entry, width=50, justify = "left")
entry_widget_name.pack(fill="both")

lbl_frame_text = LabelFrame(root, text="Enter the text here", padx=5, pady=5, height=260)
lbl_frame_text.pack(padx=10, pady=5, fill="both", expand=True)

text_widget_name = Text(lbl_frame_text)
text_widget_name.pack(fill="both", expand=True)

scrollbar = Scrollbar(text_widget_name, orient="vertical", command=text_widget_name.yview, cursor="arrow")
scrollbar.pack(fill="y", side="right")
text_widget_name.config(yscrollcommand=scrollbar.set)

button_name = Button(root, text="Search", command=search_words, padx=5, pady=5)
button_name.pack()
root.mainloop()