Search for words/letters in the text widget (tkinter)

后端 未结 2 601
孤城傲影
孤城傲影 2021-01-23 12:38

How would I go about adding a search function that searches for text in the text widget? * Search from user input

def openFile():
    global text
    artiststxt          


        
相关标签:
2条回答
  • 2021-01-23 13:23

    I make this piece of code using your post Brian Fuller, I hope it will help you, as it helped me, and others too..

    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()
    
    0 讨论(0)
  • 2021-01-23 13:43

    Okay, I figured it out. Took some time but well worth it.

    First we make an entry box in the window and we bind it with the enter key and put a .get event

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

    When enter key is pressed, we go to 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()
    
    0 讨论(0)
提交回复
热议问题