Tkinter实现模糊查询
import tkinter as tk class Test(): def __init__(self): self.root = tk.Tk() self.frm1 = tk.Frame(self.root) self.entry_value = tk.StringVar() self.entry1 = tk.Entry(self.frm1,textvariable = self.entry_value) self.listbox1 = tk.Listbox(self.frm1) self.base_selection_list = ["A","AB","ABCD","ABCDE","ABCDEF"] #self.set_list_value(self.base_selection_list) self.entry1.grid(column=0, row=0, padx=10) self.listbox1.grid(column=0, row=1, padx=10) self.listbox1.grid_remove() self.frm1.pack() self.entry1.bind("<KeyRelease>", self.entry_change) self.entry1.bind("<Button-3>", self.hide_list) self.listbox1.bind("<<ListboxSelect>>", self.list_select) self.root.mainloop() #有输入变化的时候,设置下拉列表,并显示列表 def entry_change(self, event): entry_value = self.entry1.get().strip() self.listbox1.delete(0, self.listbox1.size()-1) new_select_list = [] for selection_info in self.base_selection_list: if (len(entry_value) > 0) and (entry_value in selection_info): new_select_list.append(selection_info) print(entry_value, new_select_list) if len(new_select_list) > 0: self.set_list_value(new_select_list) #显示出来 self.listbox1.grid() else: self.listbox1.grid_remove() def hide_list(self, event): self.listbox1.grid_remove() #选择完下拉列表后,直接隐藏 def list_select(self, event): index_num = self.listbox1.curselection()[0] select_value = self.listbox1.get(index_num) self.entry_value.set(select_value) #隐藏起来 self.listbox1.grid_remove() def set_list_value(self, value_list): #["A","AB","ABCD","ABCDE","ABCDEF"] for item in value_list: self.listbox1.insert("end",item) app = Test()