Skip to content

Instantly share code, notes, and snippets.

Forked from evoL/gist:1650115
Created October 13, 2012 12:54
Show Gist options
  • Save guettli/3884560 to your computer and use it in GitHub Desktop.
Save guettli/3884560 to your computer and use it in GitHub Desktop.
Gtk Multi Autocomplete
import pygtk
import gobject
import gtk
from gtk import gdk
class EntryMultiCompletion(gtk.Entry):
def __init__(self):
self.completion = gtk.EntryCompletion()
# customize the matching function to match multiple space
# separated words
self.completion.set_match_func(self.match_func, None)
# handle the match-selected signal, raised when a completion
# is selected from the popup
self.completion.connect("match-selected", self.on_completion_match)
def match_func(self, completion, key_string, iter, data):
model = self.completion.get_model()
# get the completion strings
modelstr = model[iter][0]
# check if the user has typed in a space char,
# get the last word and check if it matches something
if " " in key_string:
last_word = key_string.split()[-1]
return modelstr.startswith(last_word)
# we have only one word typed
return modelstr.startswith(key_string)
def on_completion_match(self, completion, model, iter):
current_text = self.get_text()
# if more than a word has been typed, we throw away the
# last one because we want to replace it with the matching word
# note: the user may have typed only a part of the entire word
# and so this step is necessary
if " " in current_text:
current_text = " ".join(current_text.split()[:-1])
current_text = "%s %s" % (current_text, model[iter][0])
current_text = model[iter][0]
# add the matching word
current_text = "%s %s" % (current_text, model[iter][0])
# set back the whole text
# move the cursor at the end
# stop the event propagation
return True
if __name__ == "__main__": # register the class as a Gtk widget gobject.type_register(EntryMultiCompletion)
win = gtk.Window()
win.connect("delete-event", gtk.main_quit)
entrycompl = EntryMultiCompletion()
liststore = gtk.ListStore(gobject.TYPE_STRING)
for word in ['abc', 'def', 'ghi', 'jkl', 'mno',
'pqr', 'stu', 'vwx', 'yz']:
print 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment