Gtk.TreeView 中的 Gtk.Entry (CellRenderer)

sim*_*mon 2 python gtk gobject pygobject gobject-introspection

我想将一个Gtk.Entry(已Gtk.EntryCompletion连接)打包到Gtk.TreeView. 有谁知道如何做到这一点?(我只需要对表格视图中的文本条目进行输入完成。)

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

sim*_*mon 5

好吧,我终于想出了如何做到这一点。

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())
Run Code Online (Sandbox Code Playgroud)

灵感来自PyGTK 常见问题解答,并适用于pygobject.