Search code examples
pythongtkpygobjectgobjectgobject-introspection

Gtk.Entry in Gtk.TreeView (CellRenderer)


I want to pack a Gtk.Entry (with Gtk.EntryCompletion hooked up) into a cell in a Gtk.TreeView. Does anyone know how this can be done? (I just need entry completion on a text entry in a tabular view.)

Do I perhaps need to subclass Gtk.CellRenderer or Gtk.CellRendererText, and override the start_editing method (or similar)? I can find examples of subclassing Gtk.CellRenderer, but not modifying the editable behaviour. I can't find the source-code for the Gtk.CellRendererText class, either.

I'm using Gobject Introspection (i.e. from gi.repository import Gio, Gtk, GLib, Gdk).


Solution

  • Okay, I finally worked out how to do this.

    class CellRendererAutoComplete(Gtk.CellRendererText):
    
        """ Text entry cell which accepts a Gtk.EntryCompletion object """
    
        __gtype_name__ = 'CellRendererAutoComplete'
    
        def __init__(self, completion):
            self.completion = completion
            Gtk.CellRendererText.__init__(self)
    
        def do_start_editing(
                   self, event, treeview, path, background_area, cell_area, flags):
            if not self.get_property('editable'):
                return
            entry = Gtk.Entry()
            entry.set_completion(self.completion)
            entry.connect('editing-done', self.editing_done, path)
            entry.show()
            entry.grab_focus()
            return entry
    
        def editing_done(self, entry, path):
            self.emit('edited', path, entry.get_text())
    

    Inspiration dervied from the PyGTK FAQ, and adapted to pygobject.