Python-在可编辑的Gtk.TreeView单元格中自动完成 [英] Python - AutoComplete in an Editable Gtk.TreeView Cell

查看:96
本文介绍了Python-在可编辑的Gtk.TreeView单元格中自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在QTable中使用QComboBox编码PyQt. QComboBox默认情况下处于自动完成状态. 我想尝试使用Gtk3在Python3中重现它.我碰到了这个例子:

I was recently coding PyQt with a QComboBox in a QTable. The QComboBox has autocomplete on by default. I was wanting to try and reproduce this in Python3 with Gtk3. I came across this example:

Gtk.Entry in Gtk.TreeView(CellRenderer)

似乎已成功将自动补全添加到Treeview中的ComboBox中.这个例子还不完整,我希望有人能给我一个完整的例子.我不知道如何将CellRendererAutoComplete类连接"到树视图中的列之一.

that seems to have successfully added an autocompletion to a ComboBox in a Treeview. The example is not complete and I was hoping someone could give me a complete working example. I don't know how I 'connect' the CellRendererAutoComplete class to one of the columns in a treeview.

我真正想要的是在可编辑的TreeView单元中(带有或不带有ComboBox的情况下)具有自动完成"功能,但是我过去从未在Gtk中遇到过这种情况.

What I really want is to have AutoCompletion in an editable TreeView cell (with or without a ComboBox), but I have never come across this in the past in Gtk.

谢谢.

这是一个代码示例:

# https://stackoverflow.com/questions/13756787/
# https://python-gtk-3-tutorial.readthedocs.io/en/latest/cellrenderers.html
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
#######################################################################
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())
#######################################################################
class CellRendererTextWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererText Example")

        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append(["Fedora", "http://fedoraproject.org/"])
        self.liststore.append(["Slackware", "http://www.slackware.com/"])
        self.liststore.append(["Sidux", "http://sidux.com/"])

        treeview = Gtk.TreeView(model=self.liststore)

        renderer_text = Gtk.CellRendererText()
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        treeview.append_column(column_text)

        renderer_editabletext = Gtk.CellRendererText()
        renderer_editabletext.set_property("editable", True)
########
# This is the problem area, I suppose, but I'm not sure
        x = CellRendererAutoComplete()     
        renderer_editabletext.connect('on_edit',x(renderer_editabletext))
########
        column_editabletext = Gtk.TreeViewColumn("Editable Text",renderer_editabletext, text=1)
        treeview.append_column(column_editabletext)

        renderer_editabletext.connect("edited", self.text_edited)

        self.add(treeview)

    def text_edited(self, widget, path, text):
        self.liststore[path][1] = text

win = CellRendererTextWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

推荐答案

因此,下面的示例显示了使用EntryCompletion的CellRendererText和CellRendererCombo.

So here is an example showing a CellRendererText and a CellRendererCombo using an EntryCompletion.

由于Treeview的复杂性,其中一个ListStore可以是Completion, Combo, and Entry之后的模型,而另一个模型可以在Treeview之后,因此您应该对Gtk.Treeview有很好的理解.请注意,此示例仅使用一个Liststore,并且仅使用CellRendererText和CellRendererColumn都使用的一个可编辑列.这使示例令人困惑,但是由于我不知道该Treeview的预期用途,因此我想起来最简单.

Because of the complexity of the Treeview, where one ListStore can be the model behind the Completion, Combo, and Entry, and another model can be behind the Treeview, you should have a very good grasp of Gtk.Treeview to understand this example. Note that this example only uses one Liststore, and only one editable column used by both the CellRendererText and the CellRendererColumn. This makes the example confusing, but it the simplest I can come up with since I do not know the intended use for this Treeview.

#!/usr/bin/python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk


class CellRendererTextWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererText Example")

        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append(["Fedora", "http://fedoraproject.org/"])
        self.liststore.append(["Slackware", "http://www.slackware.com/"])
        self.liststore.append(["Sidux", "http://sidux.com/"])

        treeview = Gtk.TreeView(model=self.liststore)

        self.completion = Gtk.EntryCompletion(model = self.liststore)
        self.completion.set_text_column(1)
        self.completion.connect('match-selected', self.renderer_match_selected)

        renderer_text = Gtk.CellRendererText()
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        treeview.append_column(column_text)

######## CellRendererText with EntryCompletion example
        renderer_text = Gtk.CellRendererText()
        renderer_text.connect('editing-started', self.renderer_text_editing_started)
        renderer_text.connect('edited', self.text_edited)
        renderer_text.set_property("editable", True)

        column_text_autocomplete = Gtk.TreeViewColumn("Editable Text", renderer_text, text=1)
        treeview.append_column(column_text_autocomplete)

######## CellRendererCombo with EntryCompletion example
        renderer_combo = Gtk.CellRendererCombo(model = self.liststore)
        renderer_combo.set_property("text-column", 1)
        renderer_combo.connect('editing-started', self.renderer_combo_editing_started)
        renderer_combo.connect('changed', self.combo_changed)
        renderer_combo.set_property("editable", True)

        column_combo_autocomplete = Gtk.TreeViewColumn("Editable Combo", renderer_combo, text=1)
        treeview.append_column(column_combo_autocomplete)


        self.add(treeview)

    def renderer_match_selected (self, completion, model, tree_iter):
        ''' beware ! the model and tree_iter passed in here are the model from the
        EntryCompletion, which may or may not be the same as the model of the Treeview '''
        text_match =  model[tree_iter][1]
        self.liststore[self.path][1] = text_match

    def renderer_text_editing_started (self, renderer, editable, path):
        ''' since the editable widget gets created for every edit, we need to 
        connect the completion to every editable upon creation '''
        editable.set_completion(self.completion)
        self.path = path # save the path for later usage

    def text_edited(self, widget, path, text):
        self.liststore[path][1] = text

    def renderer_combo_editing_started (self, renderer, combo, path):
        ''' since the editable widget gets created for every edit, we need to 
        connect the completion to every editable upon creation '''
        editable = combo.get_child() # get the entry of the combobox
        editable.set_completion(self.completion)
        self.path = path # save the path for later usage

    def combo_changed (self, combo, path, tree_iter):
        ''' the path is from the treeview and the tree_iter is from the model 
        of the combobox which may or may not be the same model as the treeview'''
        combo_model = combo.get_property('model')
        text = combo_model[tree_iter][1]
        self.liststore[path][1] = text

win = CellRendererTextWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

在官方文档中,明确指出editing-started的目的是添加EntryCompletion等.在我在文档中没有发现任何小提示之前,我也将Gtk.CellRendererText子类化.

In the official docs, it is explicitly stated that the purpose of editing-started is to add a EntryCompletion and etc. I also subclassed Gtk.CellRendererText before I found that little hint in the docs.

这篇关于Python-在可编辑的Gtk.TreeView单元格中自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆