Question

I can't figure out what else I need for the combobox to select from my list as I type. Eventually I'm going to add SetInsertionPoint. But for now, my item selected is 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
Was it helpful?

Solution

This other SO answer has a custom control that might work for you:

You may also get ideas from this wxPython wiki article

I also noticed that the masked combo box may have this feature itself, according to the docs: http://www.wxpython.org/docs/api/wx.lib.masked.combobox-module.html which says the following

BaseMaskedComboBox - Base class for generic masked edit comboboxes; allows auto-complete of values.

OTHER TIPS

To use the GetSelection() alone, you need to set the ComboBox to read only. It's a nice way to query by hitting one charactor. Using SetInsertionPoint and SetMark keeps the curser to the next string of your query. I used the example Mike suggested •Auto-Completion In wxPython wxComboBox and modified my code to used these instances. Since I always use boxsizers and open functions I needed to do away with the wx.EVT_TEXT event. Here's how it works:

##  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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top