How to fix StringVar.get() issue

前端 未结 1 359
庸人自扰
庸人自扰 2021-01-23 15:21

I am trying to make autocomplete GUI (like Google\'s) in Tkinter using StringVar. I defined a callback function , where i used StringVar.get(), where I for different input in En

1条回答
  •  既然无缘
    2021-01-23 16:01

    It is because all matched items for the first letter have been removed from the search list. You should use a cloned search list in callback(). Also don't create new list to show the result list, create the result list once and update its content in callback(). Furthermore, sort the search list beforehand:

    def callback(sv, wordlist, num):
        result.delete(0, END) # remove previous result
        a = sv.get().strip()
        if a:
            pom_list = wordlist[:]  # copy of search list
            #lexicographic_sort(pom_list)  # should sort the list beforehand
            x = binary_search(a, pom_list)
            while x != -1 and num > 0:
                result.insert(END, x)
                pom_list.remove(x)
                num -= 1
                x = binary_search(a, pom_list)
    
    ...
    
    lexicographic_sort(wordlist)
    sv = StringVar()
    sv.trace("w", lambda *args, sv=sv: callback(sv, wordlist, num))
    
    ...
    
    result = Listbox(root, width=70)
    result.grid(row=2, column=5)
    

    0 讨论(0)
提交回复
热议问题