问题
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
).
回答1:
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
.
回答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.
来源:https://stackoverflow.com/questions/13756787/gtk-entry-in-gtk-treeview-cellrenderer