簡體   English   中英

具有自動完成wxPython的ComboBox

[英]ComboBox with autocomplete wxPython

我希望有一個具有自動完成功能的ComboBox,因為我有一個超過1000個項目的列表,並且希望能夠通過只在ComboBox中保留部分項目字符串來選擇一個元素而不必遍歷整個列表。

我一直在環顧四周,這個問題已被多次回答,我什至在這里檢查了上一個問題的以下鏈接:

和另一個鏈接:

但是,當我嘗試運行示例代碼時,總是出現錯誤:“模塊'wx'沒有屬性'SimpleHtmlListBox'/'HtmlListBox'”。

錯誤的原因可能是什么? 還有其他實現自動完成組合框的方法嗎?

我已經轉換了您提供的第一個鏈接中的代碼,以使其在python 3 wxPython Phoenix中工作。 添加了一些更改以使組合框更好地工作。 第一個comboBox已在Mac OSX中進行了測試。 但是,當我對其進行測試時,Linux gtk +無法正常工作,因此我在第二行中使用TextCtrl作為comboBox的篩選器來創建工作。

import wx

class PromptingComboBox(wx.ComboBox) :
    def __init__(self, parent, choices=[], style=0, **par):
        wx.ComboBox.__init__(self, parent, wx.ID_ANY, style=style|wx.CB_DROPDOWN, choices=choices, **par)
        self.choices = choices
        self.Bind(wx.EVT_TEXT, self.OnText)
        self.Bind(wx.EVT_KEY_DOWN, self.OnPress)
        self.ignoreEvtText = False
        self.deleteKey = False

    def OnPress(self, event):
        if event.GetKeyCode() == 8:
            self.deleteKey = True
        event.Skip()

    def OnText(self, event):
        currentText = event.GetString()
        if self.ignoreEvtText:
            self.ignoreEvtText = False
            return
        if self.deleteKey:
            self.deleteKey = False
            if self.preFound:
                currentText =  currentText[:-1]

        self.preFound = False
        for choice in self.choices :
            if choice.startswith(currentText):
                self.ignoreEvtText = True
                self.SetValue(choice)
                self.SetInsertionPoint(len(currentText))
                self.SetTextSelection(len(currentText), len(choice))
                self.preFound = True
                break

class TrialPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)

        choices = ['grandmother', 'grandfather', 'cousin', 'aunt', 'uncle', 'grandson', 'granddaughter']
        for relative in ['mother', 'father', 'sister', 'brother', 'daughter', 'son']:
            choices.extend(self.derivedRelatives(relative))
        self.choices = choices = sorted(choices)

        mainSizer = wx.FlexGridSizer(2, 2, 5, 10)
        self.SetSizer(mainSizer)

        mainSizer.Add(wx.StaticText(
            self, -1, "Worked in Mac - python 3 - wx phoenix"))
        cb1 = PromptingComboBox(self, choices=choices)
        mainSizer.Add(cb1)

        mainSizer.Add(wx.StaticText(self, -1, "Work arround in Linux-gtk"))
        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        mainSizer.Add(sizer2)
        filterCtrl = wx.TextCtrl(self, -1, size=(150, -1))
        filterCtrl.Bind(wx.EVT_TEXT, self.OnFilter)
        sizer2.Add(filterCtrl)
        self.cb2 = wx.ComboBox(self, -1, size=(150, -1), choices=choices)
        sizer2.Add(self.cb2)


    def derivedRelatives(self, relative):
        return [relative, 'step' + relative, relative + '-in-law']

    def OnFilter(self, event):
        currentText = event.GetString().upper()
        tmpChoices = [c for c in self.choices if c.startswith(currentText)]
        if tmpChoices != []:
            self.cb2.SetItems(tmpChoices)
            self.cb2.SetValue(tmpChoices[0])
        else:
            self.cb2.SetValue('')
            self.cb2.SetItems([])

if __name__ == '__main__':
    app = wx.App(False)
    frame = wx.Frame (None, -1, 'Demo PromptingComboBox Control and Work around',
                      size=(700, 400))
    TrialPanel(frame)
    frame.Show()
    app.MainLoop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM