Is there a way to use fuzzy selection to narrow the choices in a wx.ComboBox (wxWidgets)

I want to populate a wx.ComboBox with a large number of items but rather than having to scroll through them all, I’d like to type a string into the ComboBox and narrow the items in the ComboBox using fuzzy search (I’m using ‘thefuzz’).

So I’ve almost got it working but there’s something amiss in the plumbing. I suspect I need to capture a wider range of events so that I can implement backspace and perhaps escape, but the main problem is that I can’t seem to manipulate the TextEntry in the ComboBox the way I’d like.

Has anyone already done this?

Here’s my code so far:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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()
</code>
<code>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() </code>
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()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật