3

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).

liberforce
  • 11,189
  • 37
  • 48
simon
  • 15,344
  • 5
  • 45
  • 67

2 Answers2

5

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.

simon
  • 15,344
  • 5
  • 45
  • 67
  • You, or anybody else coming here, may wish to check out my complete (read, complex) answer [here](https://stackoverflow.com/questions/57136209/python-autocomplete-in-an-editable-gtk-treeview-cell/57137888?noredirect=1#comment100791715_57137888), that doesn't need subclassing. – theGtknerd Jul 22 '19 at 09:43
2

You shouldn't have to subclass, GTK+ rarely requires this. It might be more practical in Python (than in C) of course, if so it should be fine.

This page shows how to enable editing, by setting the editable property to TRUE.

You can use gtk_tree_view_set_cursor() to move the cursor to a cell and also to start editing programmatically.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    thanks, but I'm aware of all that and it doesn't really address my question at all. i need to intercept or override the `on_edit` event so that I can modify the behaviour, to hook the `Gtk.Entry` up with completion or replace it with my own, completion-enabled one. – simon Dec 07 '12 at 18:13
  • i found what i was looking for this morning (see my answer). it think it was a night's sleep that helped, as much as anything, but +1 to you for posting something that made me start thinking about this again this morning :) thanks! – simon Dec 07 '12 at 19:24
  • i don't suppose you would be able to help with my [other problem](http://stackoverflow.com/questions/13736695/unpacking-gvariant-in-javascript), would you? – simon Dec 07 '12 at 19:26