2012-12-07 39 views
1

我想将Gtk.Entry(连接Gtk.EntryCompletion)打包到Gtk.TreeView中的单元格中。有谁知道这可以做到吗? (我只需要在表格视图中输入完成的文本条目。)Gtk.TreeView中的Gtk.Entry(CellRenderer)

我可能需要子类Gtk.CellRendererGtk.CellRendererText,并覆盖start_editing方法(或类似方法)吗?我可以找到子类Gtk.CellRenderer的示例,但不能修改可编​​辑的行为。我也找不到Gtk.CellRendererText类的源代码。

我正在使用Gobject内省(即from gi.repository import Gio, Gtk, GLib, Gdk)。

回答

3

好吧,我终于弄清楚了如何做到这一点。

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

启示从PyGTK FAQ dervied,并适于pygobject

1

你不应该子类化,GTK +很少需要这个。当然,它可能在Python中比C更实际,如果这样的话应该没问题。

This page显示如何通过将editable属性设置为TRUE来启用编辑。

您可以使用gtk_tree_view_set_cursor()将光标移动到单元格,也可以通过编程方式开始编辑。

+1

谢谢,但我意识到这一切,它并没有真正解决我的问题。我需要拦截或重写'on_edit'事件,以便我可以修改行为,将Gtk.Entry挂钩完成或将其替换为我自己的启用完成的行为。 – simon

+0

我发现我今天早上要找的东西(请参阅我的回答)。它认为这是一个晚上的睡眠,就像任何事情一样帮助,但是+1发布给我的东西让我今天上午再次开始考虑这个问题:)谢谢! – simon

+0

我不认为你能够帮助我的[其他问题](http://stackoverflow.com/questions/13736695/unpacking-gvariant-in-javascript),你会吗? – simon

相关问题