FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 14.24 | Next Entry >>

14.24. How do I add completion support for an entry?

Here is a simple example that demonstrates how to add entry completion to a GtkEntry:

 import gtk

 COL_TEXT = 0

 class CompletedEntry(gtk.Entry):
     def __init__(self):
         gtk.Entry.__init__(self)
         completion = gtk.EntryCompletion()
         completion.set_match_func(self.match_func)
         completion.connect("match-selected",
                             self.on_completion_match)
         completion.set_model(gtk.ListStore(str))
         completion.set_text_column(COL_TEXT)
         self.set_completion(completion)

     def match_func(self, completion, key, iter):
         model = completion.get_model()
         return model[iter][COL_TEXT].startswith(self.get_text())

     def on_completion_match(self, completion, model, iter):
         self.set_text(model[iter][COL_TEXT])
         self.set_position(-1)

     def add_words(self, words):
         model = self.get_completion().get_model()
         for word in words:
             model.append([word])

 if __name__ == "__main__":
     win = gtk.Window()
     win.connect('delete-event', gtk.main_quit)

     entry = CompletedEntry()
     entry.add_words(['abc', 'def', 'ghi', 'jkl', 'mno',
                      'pqr', 'stu', 'vwx', 'yz'])
     win.add(entry)
     win.show_all()

     gtk.main()

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org