tkinter中combobox实现模糊匹配

from tkinter import *
from tkinter.ttk import Combobox
from fuzzywuzzy import fuzz

root = Tk()
root.geometry('200x200')

options = ['Option 1', 'Option 2', 'Option 3']
combo = Combobox(root, values=options, state='read-write', width=15, height=5)
combo.pack()

def on_key_release(event):
	text = event.widget.get()
	if len(text) > 0:
		matches = []
		for option in options:
			ratio = fuzz.ratio(text.lower(), option.lower())
			if ratio >= 50:
				matches.append(option)
		combo['values'] = matches
	else:
		combo['values'] = options

combo.bind('<KeyRelease>', on_key_release)

root.mainloop()

  

posted on 2023-03-19 14:11  帅胡  阅读(138)  评论(0编辑  收藏  举报

导航