Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am making a todo like application with indicator support using glade/python/pygtk/glade. I am making a form to save a Todo item. Each todo will belong to 0 or more projects.

When a user enters a project name in a text input field I want it to auto-complete based on existing projects. This widget should behave similarly to the text input when completing the ask a question form on this site (askubuntu.com). Below is a screenshot of what I mean.

For example when the user wants to add a todo under the projects "quickly" once the user starts typing "qu*" it should suggest all available projects that start with "qu" (e.g. "quick", "quickly", "queue", "quack" etc.). The user may press the down arrow to select the correct option or continue typing. When the user presses spacebar, it should start with a new suggestion.

Is there a control/widget in pyGTK/quickly that I can use to accomplish this? Bonus points if I can also list a number next to each suggestion with the number of todo items that belong to this project, similar to the screenshot below.

Auto-completion on AskUbuntu.com

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Here's a simple example:

from gi.repository import Gtk

class MyExample(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("delete-event", Gtk.main_quit)

        liststore = Gtk.ListStore(str)
        for match in ["test1", "test2", "test3", "spam", "foo", "eggs", "bar"]:
            liststore.append([match])

        completion = Gtk.EntryCompletion()
        completion.set_model(liststore)
        completion.set_text_column(0)

        entry = Gtk.Entry()
        entry.set_completion(completion)
        self.add(entry)
        self.show_all()

if __name__ == "__main__":
    app = MyExample()
    Gtk.main()

You'll just have to dynamicly fill the liststore with the values you want. Displaying a number next to it can be as trivial as appending it to the string you're adding to the liststore or you can create a second column in it. This all depends on your code and isn't possible to answer, but this should get you going.

You can even create most of the above code in Glade if you want.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.