EntryCompletion

An EntryCompletion can be attached to an Entry widget to provide suggestions which match the characters entered.

Constructor

The EntryCompletion can be constructed using the following:

entrycompletion = Gtk.EntryCompletion()

Methods

Once the EntryCompletion has been constructed, a model needs to be attached. This is used to store the potential values which can be matched:

entrycompletion.set_model(model)

The model should be the name of a ListStore object.

The text column must also be set which refers to which column within the ListStore the potentially matches should be listed:

entrycompletion.set_text_column(column)

To prevent completion lookups occuring before a certain number have characters have been entered, the following method can be used:

entrycompletion.set_minimum_key_length(length)

The length parameter should be set to an integer value.

To cycle through the position completions within the Entry:

entrycompletion.set_inline_completion(inline)

When inline is set to True, the user can cycle through the matching completions by tabbing.

Alternatively, there is an option to provide a list of completions popped up in a popup window with:

entrycompletion.set_popup_completion(popup)

Example

Below is an example of a EntryCompletion:

#!/usr/bin/env python3

from gi.repository import Gtk

class EntryCompletion(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect("destroy", Gtk.main_quit)

        grid = Gtk.Grid()
        self.add(grid)

        liststore = Gtk.ListStore(str)

        for item in ["Andrew", "Natalie", "Mark", "David", "Daniel", "Anita", "Matthew"]:
            liststore.append([item])

        self.entrycompletion = Gtk.EntryCompletion()
        self.entrycompletion.set_model(liststore)
        self.entrycompletion.set_text_column(0)

        entry = Gtk.Entry()
        entry.set_completion(self.entrycompletion)
        grid.attach(entry, 0, 0, 1, 1)

        radiobuttonPopup = Gtk.RadioButton("Popup Completion")
        radiobuttonPopup.mode = 0
        radiobuttonPopup.connect("toggled", self.on_radiobutton_toggled)
        grid.attach(radiobuttonPopup, 0, 1, 1, 1)
        radiobuttonInline = Gtk.RadioButton("Inline Completion")
        radiobuttonInline.mode = 1
        radiobuttonInline.join_group(radiobuttonPopup)
        radiobuttonInline.connect("toggled", self.on_radiobutton_toggled)
        grid.attach(radiobuttonInline, 0, 2, 1, 1)

    def on_radiobutton_toggled(self, radiobutton):
        if radiobutton.get_active():
            if radiobutton.mode == 0:
                self.entrycompletion.set_popup_completion(True)
                self.entrycompletion.set_inline_completion(False)
            elif radiobutton.mode == 1:
                self.entrycompletion.set_popup_completion(False)
                self.entrycompletion.set_inline_completion(True)


window = EntryCompletion()
window.show_all()

Gtk.main()

Download: EntryCompletion