【问题标题】:Gtk.Entry in Gtk.TreeView (CellRenderer)Gtk.TreeView 中的 Gtk.Entry (CellRenderer)
【发布时间】:2012-11-25 06:13:42
【问题描述】:

我想将Gtk.Entry(连接了Gtk.EntryCompletion)打包到Gtk.TreeView 的单元格中。有谁知道如何做到这一点? (我只需要在表格视图中的文本条目上完成条目。)

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

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

【问题讨论】:

    标签: python gtk pygobject gobject gobject-introspection


    【解决方案1】:

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

    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,改编为pygobject

    【讨论】:

    • 您或其他任何来到这里的人都可能希望查看我的完整(阅读、复杂)答案here,它不需要子类化。
    【解决方案2】:

    您不必子类化,GTK+ 很少需要这个。当然,在 Python 中(比在 C 中)可能更实用,如果是这样应该没问题。

    This page 展示了如何通过将@987654322@ 属性设置为TRUE 来启用编辑。

    您可以使用@987654323@ 将光标移动到单元格并开始以编程方式进行编辑。

    【讨论】:

    • 谢谢,但我知道这一切,它根本没有真正解决我的问题。我需要拦截或覆盖on_edit 事件,以便我可以修改行为,将Gtk.Entry 与完成挂钩,或者将其替换为我自己的启用完成的事件。
    • 我今天早上找到了我想要的东西(见我的回答)。它认为这是一个晚上的睡眠,这和其他任何事情一样有帮助,但是 +1 你发布了一些让我今天早上再次开始思考这个问题的东西:) 谢谢!
    • 我不认为你能帮我处理我的other problem,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2021-09-27
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多