Combobox GetSelection组合框

Combobox GetSelection组合框,combobox,wxpython,getselection,Combobox,Wxpython,Getselection,我想不出组合框还需要选择什么 从我的列表中键入。最后,我将添加SetInsertionPoint。 但现在,我选择的项目是allways-1 self.filter = wx.ComboBox(self, wx.ID_ANY, choices = '', style=wx.CB_DROPDOWN) def OnTextChanged(self, event): sel = self.filter.GetSelection() print 'OnItemSelected %s'

我想不出组合框还需要选择什么 从我的列表中键入。最后,我将添加SetInsertionPoint。 但现在,我选择的项目是allways-1

self.filter = wx.ComboBox(self, wx.ID_ANY, choices = '', style=wx.CB_DROPDOWN)

def OnTextChanged(self, event):
    sel = self.filter.GetSelection()
    print 'OnItemSelected %s' % sel

此其他SO答案有一个自定义控件,可能适合您:

你也可以从这篇wxpythonwiki文章中得到一些想法

我还注意到蒙面组合框本身可能有这个特性,根据文档:上面说

BaseMaskedComboBox—通用屏蔽编辑组合框的基类;允许自动完成值。

要单独使用GetSelection(),需要将组合框设置为只读。这是一个很好的旅行方式 通过点击一个字符进行查询。使用SetInsertionPoint和SetMark将游标保留到查询的下一个字符串。我使用了Mike建议的示例•wxPython wxComboBox中的自动完成 并修改了我的代码以使用这些实例。因为我总是使用boxsizer和open函数,所以我需要去掉wx.EVT_文本事件。下面是它的工作原理:

##  copy/paste to text file
'''
73,"N WASHINGTON ST"
95,"BRIAR CREEK RD"
97,"N INDEPENDENCE AVE"
09,"N ADAMS ST"
13,"N JEFFERSON ST"
19,"N MADISON ST"
21,"QUAIL CREEK DR"
24,"INDIAN DR"
12,"CHEROKEE TRAIL"
50,"CORONADO TRAIL"
'''
import wx, os
from cStringIO import StringIO
import csv

class MainFrame(wx.Frame):
    def __init__(self, parent, choices=[], style=0):
        wx.Frame.__init__(self,None,wx.ID_ANY,title='test combo autocomplete',size=(225, 70))

        self.vbox= wx.BoxSizer(wx.VERTICAL)
        self.background = wx.Panel(self)
        self.OpenDir = wx.TextCtrl(self,style=wx.PROCESS_ENTER|wx.TE_CENTRE)
        self.filter = wx.ComboBox(self, wx.ID_ANY, style=wx.CB_DROPDOWN)
        self.OpenDir.Bind(wx.EVT_LEFT_UP,self.OnChooseRoot)
        self.filter.Bind(wx.EVT_TEXT, self.OnTextChanged)

        hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
        hsizer1.Add(self.OpenDir,1)        
        hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
        hsizer2.Add(self.filter,1)

        self.vbox.Add(hsizer1,proportion = 0,flag = wx.EXPAND)
        self.vbox.Add(hsizer2,proportion = 0,flag = wx.EXPAND) 
        self.SetSizer(self.vbox)
        self.Show()
        self.OpenDir.SetValue("click to open directory")

    def OnTextChanged(self, event):
        def refresh():
            wnd = self.filter
            currentText = event.GetString()
            while wnd:
                print currentText
                wnd.Layout()
                print wnd.Layout()
                wnd = wnd.GetParent()
                self.filter.SetInsertionPoint(len(currentText))
                self.filter.SetMark(len(currentText), len(self.choices))
            self.filter.Refresh()
        refresh()
        event.Skip()

    def OnChooseRoot(self, event):
        self.dirname="" 
        dlg = wx.FileDialog(self, "choose a file to open", self.dirname,
                            "", "*.*", wx.OPEN) 
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            self.pathname = dlg.GetPath()
            self.f = open(os.path.join(self.dirname, self.filename), 'r')  
            self.text = self.f.read()
            labeltop = self.dirname + '\\'
            self.OpenDir.SetValue(labeltop + self.filename)

            sources = [StringIO(self.text)]
            for i, source in enumerate(sources):  
                c = list(csv.reader(source))                
                self.choices = [x[1] for x in c]
                self.filter.SetItems(self.choices)

app = wx.App(redirect=False)
frame = MainFrame(None)
app.MainLoop()