import tkinter as tk class Test(): def __init__(self): self.root = tk.Tk() self.entry_value = tk.StringVar() self.entry1 = tk.Entry(self.root,textvariable = self.entry_value) self.listbox1 = tk.Listbox(self.root) 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.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() print(entry_value) self.listbox1.delete(0, self.listbox1.size()-1) new_select_list = [] for selection_info in self.base_selection_list: if entry_value in selection_info: new_select_list.append(selection_info) self.set_list_value(new_select_list) #显示出来 self.listbox1.grid() 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()
标签:Tkinter,listbox1,self,模糊,list,value,查询,entry,select From: https://www.cnblogs.com/hushaojun/p/17195548.html