Name

gtk.EntryCompletion — completion functionality for gtk.Entry (new in PyGTK 2.4)

Synopsis

class gtk.EntryCompletion(gobject.GObject, gtk.CellLayout):
    gtk.EntryCompletion()
def get_entry()
def set_model(model)
def get_model()
def set_match_func(func, func_data)
def set_minimum_key_length(length)
def get_minimum_key_length()
def complete()
def insert_action_text(index, text)
def insert_action_markup(index, markup)
def delete_action(index)
def set_text_column(column)

Ancestry

+-- gobject.GObject
  +-- gtk.EntryCompletion (implements gtk.CellLayout)

Properties

"minimum-key-length"Read-WriteMinimum length of the search key in order to look up matches.
"model"Read-WriteThe gtk.TreeModel to find matches in.

Signal Prototypes

"action-activated" def callback(completion, index, user_param1, ...)
"match-selected" def callback(completion, model, iter, user_param1, ...)

Description

Note

This widget is available in PyGTK 2.4 and above.

gtk.EntryCompletion is an auxiliary object to be used in conjunction with gtk.Entry to provide completion functionality. It implements the gtk.CellLayout interface, to allow the user to add extra cells to the popup display of completions.

To add completion functionality to an entry, use the gtk.Entry.set_completion() method. In addition to regular completion matches, that will be inserted into the entry when they are selected, gtk.EntryCompletion also allows "actions" to be displayed in the popup window below any completions. Their appearance is similar to menuitems, to differentiate them clearly from completion strings. When an action is selected, the "action-activated" signal is emitted.

A gtk.TreeModel (e.g. a gtk.ListStore) containing the completion strings is associated with the gtk.EntryCompletion using the set_model() method. The tree model column containing the completion strings can be set using the convenience method set_text_column() that also creates a gtk.CellRendererText and packs it into the entry completion.

Otherwise, you can create gtk.CellRenderer objects and pack them into the gtk.EntryCompletion using the gtk.CellLayout methods gtk.CellLayout.pack_start() or gtk.CellLayout.pack_start(). However, you will also have to define a match function and set it with the set_match_func() method.

If you wanted to create a completion list with the strings to insert and some additional info e.g. an icon or description you could do something like:

  entry = gtk.Entry()
  completion = gtk.EntryCompletion()
  entry.set_completion(completion)
  liststore = gtk.ListStore(gobject.TYPE_STRING, gtk.gdk.Pixbuf)
  completion.set_model(liststore)
  pixbufcell = gtk.CellRendererPixbuf()
  completion.pack_start(pixbufcell)
  completion.add_attribute(pixbufcell, 'pixbuf', 1)
  # create a gtk.CellRendererText and pack it in the completion. Also set the
  # 'text' attribute
  completion.set_text_column(0)
  # load up the liststore with string - pixbuf data - assuming pixbuf created
  liststore.append(['string text', pixbuf])

This will create an entry that will display a pixbuf and the text string during completion.

Actions are easily managed using the insert_action_text(), insert_action_markup() and delete_action() methods.

Constructor

    gtk.EntryCompletion()
Returns :A newly created gtk.EntryCompletion object.

Note

This constructor is available in PyGTK 2.4 and above.

Creates a new gtk.EntryCompletion object.

Methods

gtk.EntryCompletion.get_entry

    def get_entry()
Returns :The gtk.Entry that the completion is attached to.

Note

This method is available in PyGTK 2.4 and above.

The get_entry() method retrieves the gtk.Entry that the entry completion is attached to.

gtk.EntryCompletion.set_model

    def set_model(model)
model :The gtk.TreeModel to use with the entry completion.

Note

This method is available in PyGTK 2.4 and above.

The set_model() method sets the gtk.TreeModel specified by model to be used with the entry completion. A previously set model will be removed before the new model is set.

gtk.EntryCompletion.get_model

    def get_model()
Returns :The current gtk.TreeModel, or None if not set.

Note

This method is available in PyGTK 2.4 and above.

The get_model() method returns the gtk.TreeModel that the entry completion is using as data source. Returns None if the model is unset.

gtk.EntryCompletion.set_match_func

    def set_match_func(func, func_data)
func :A function to be used.
func_data :The user data for func.

Note

This method is available in PyGTK 2.4 and above.

The set_match_func() method sets the match function specified by func. The match function is used by the entry completion to determine if a row of the associated tree model should be in the completion list.

The signature of the match function is:

  def match_func(completion, key_string, iter, func_data):

where completion is the gtk.EntryCompletion that the match function is invoked on, key_string is the current contents of the gtk.Entry to be matched, iter is a gtk.TreeIter pointing at a row in the gtk.TreeModel associated with completion and func_data is the data specified when the set_match_func() method was called. The match function should return TRUE if the completion string should be displayed; otherwise, FALSE.

A simple example match function is:

  # Assumes that the func_data is set to the number of the text column in the
  # model.
  def match_func(completion, key, iter, column):
    model = completion.get_model()
    text = model.get_value(iter, column)
    if text.startswith(key):
      return True
    return False

You must use the set_match_func() method to display completions if you don't use the set_text_column() method.

gtk.EntryCompletion.set_minimum_key_length

    def set_minimum_key_length(length)
length :The minimum length of the key string in order to start completing.

Note

This method is available in PyGTK 2.4 and above.

The set_minimum_key_length() method sets the minimum length of the search key to the value specified by length. This means that the key string (contents of the gtk.Entry) must be at least length characters before a completion list will be displayed. This is useful for long lists, where completing using a small key will take too much time and will likely return too large a dataset.

gtk.EntryCompletion.get_minimum_key_length

    def get_minimum_key_length()
Returns :The currently used minimum key length.

Note

This method is available in PyGTK 2.4 and above.

The get_minimum_key_length() method returns the minimum key length set for the entry completion. See the set_minimum_key_length() method for more information.

gtk.EntryCompletion.complete

    def complete()

Note

This method is available in PyGTK 2.4 and above.

The complete() method requests a completion operation, i.e. a refiltering of the current list with completions, using the current key. The completion list view will be updated accordingly.

gtk.EntryCompletion.insert_action_text

    def insert_action_text(index, text)
index :The index in the action list where the item should be inserted.
text :The text of the item to insert.

Note

This method is available in PyGTK 2.4 and above.

The inset_action_text() method inserts an action in the action item list of the entry completion at the position specified by index with the text specified by text. If you want the action item to have markup, use the gtk.EntryCompletion.insert_action_markup() method.

gtk.EntryCompletion.insert_action_markup

    def insert_action_markup(index, markup)
index :The index in the action list where the item should be inserted.
markup :The Pango markup of the item to insert.

Note

This method is available in PyGTK 2.4 and above.

The insert_action_markup() method inserts an action item in the action item list of the entry completion at the position specified by index with the Pango markup specified by markup.

gtk.EntryCompletion.delete_action

    def delete_action(index)
index :The index of the item to delete.

Note

This method is available in PyGTK 2.4 and above.

The delete_action() method deletes the action item at the position in the action item list specified by index.

gtk.EntryCompletion.set_text_column

    def set_text_column(column)
column :The column in the model to get strings from.

Note

This method is available in PyGTK 2.4 and above.

The set_text_column() method is a convenience method for setting up the most common completion scenario: a completion list with just strings. This method creates and adds a gtk.CellRendererText using the column specified by column as the source for completion strings. If you don't use this method you will have to install a gtk.CellRendererText in the entry completion and set a match function using the set_match_func() method to display the completion strings.

Signals

The "action-activated" Signal

    def callback(completion, index, user_param1, ...)
completion :the entry completion that received the signal
index :the index of the action item that was activated.
user_param1 :the first user parameter (if any) specified with the connect() method
... :additional user parameters (if any)

The "action-activated" signal is emitted when an action item is selected from the popup action list.

The "match-selected" Signal

    def callback(completion, model, iter, user_param1, ...)
completion :the entry completion that received the signal
model :the gtk.TreeModel that iter points into.
iter :a gtk.TreeIter pointing at the selection completion string row in model.
user_param1 :the first user parameter (if any) specified with the connect() method
... :additional user parameters (if any)

The "match-selected" signal is emitted when a completion string was selected from the completion list. iter points at the row in model that contains the completion string.