如何基于thefuzz在wx.ComboBox中实现模糊搜索筛选选项
基于wx.ComboBox的模糊搜索实现问题
我希望为wx.ComboBox填充大量选项,无需逐一滚动查找,而是通过在ComboBox中输入字符串,利用thefuzz库进行模糊搜索来缩小选项范围。目前已接近实现该功能,但仍存在一些问题:需要捕获更多类型的事件以实现退格、Esc键功能,且无法按预期操作ComboBox中的文本输入框。请问是否有人已经实现过该功能?
以下是我目前的代码:
import wx from thefuzz import process class ThingSelector(wx.Frame): def __init__(self, parent, title, thing_descriptions): super(ThingSelector, self).__init__(parent, title=title, size=(400, 200)) self.thing_descriptions = thing_descriptions self.init_ui() def init_ui(self): panel = wx.Panel(self) self.thing_choice = wx.ComboBox(panel, choices=self.thing_descriptions) self.thing_choice_block = False self.thing_choice_filter = "" self.thing_choice.Bind(wx.EVT_TEXT, self.on_text) accept_button = wx.Button(panel, label="Accept") accept_button.Bind(wx.EVT_BUTTON, self.accept_button_click) cancel_button = wx.Button(panel, label="Cancel") cancel_button.Bind(wx.EVT_BUTTON, self.on_cancel_button) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.thing_choice, 0, wx.ALL | wx.EXPAND, 5) buttons = wx.BoxSizer(wx.HORIZONTAL) sizer.Add(buttons, 0, wx.ALL | wx.EXPAND, 5) buttons.Add(accept_button, 0, wx.ALL | wx.EXPAND, 5) buttons.Add(cancel_button, 0, wx.ALL | wx.EXPAND, 5) panel.SetSizer(sizer) self.Show() def update_choices(self, filter): matches = process.extractBests(filter, self.thing_descriptions, limit=10) self.thing_choice.SetItems([match[0] for match in matches]) self.thing_choice.SetInsertionPointEnd() self.thing_choice.Popup() def on_text(self, event): # avoid recursion when I call ChangeValue if not self.thing_choice_block: self.thing_choice_block = True current_text = event.GetString() if len(current_text) == 1: self.thing_choice_filter += current_text self.update_choices(self.thing_choice_filter) self.thing_choice_block = False else: self.thing_choice_filter = "" self.thing_choice.ChangeValue(current_text) def accept_button_click(self, event): selected_thing = "" selection = self.thing_choice.GetSelection() if selection != wx.NOT_FOUND: selected_thing = self.thing_choice.GetString(selection) print(selected_thing) return def on_cancel_button(self, event): self.Close() # Create list of 'thing's: thing_descriptions = [ "aaa", "bbb", "ccc" ] # GUI setup app = wx.App(False) frame = ThingSelector(None, title="Select Thing", thing_descriptions=thing_descriptions) app.MainLoop()
内容的提问来源于stack exchange,提问作者wef
相关产品推荐
相关产品推荐

