本帖最后由 qq1151985918 于 2022-6-27 23:43 编辑
以前我倒是写过一个模糊搜索,不过不是下拉框。combobox有很多功能实现不了
把代码给你看看对你有没有帮助吧
import tkinter
def newlistbox(oldlist):
gettext = entry.get()
newlist = sorted(oldlist, key=str.lower)
listbox.delete(0, 'end')
for each in newlist:
if gettext in each:
listbox.insert('end', each)
listbox.selection_set(0)
def newtext(event):
v = event.widget.curselection()
text = event.widget.get(v)
entry.delete(0, 'end')
entry.insert(0, text )
def textchange(event):
newlist = []
gettext = event.widget.get().strip().lower()
if event.keysym:
if gettext:
for each in testlist:
if gettext in each.lower():
newlist.append(each)
else:
newlist = testlist
newlistbox(newlist)
root = tkinter.Tk()
entry = tkinter.Entry(root)
entry.bind('<KeyRelease>', textchange)
listbox = tkinter.Listbox(root)
listbox.bind('<<ListboxSelect>>', newtext)
entry.grid(row=0, column=0)
listbox.grid(row=1, column=0)
testlist = ('apple', 'banana', 'Cranberry',
'cherry', 'Orange', 'pear',
'Lichee', 'Strawberry','grape' )
newlistbox(testlist)
root.mainloop()
|