Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 是否可以在自动完成弹出列表(Gtk3)中显示图像_Image_Python 2.7_Autocomplete_Gtk3 - Fatal编程技术网

Image 是否可以在自动完成弹出列表(Gtk3)中显示图像

Image 是否可以在自动完成弹出列表(Gtk3)中显示图像,image,python-2.7,autocomplete,gtk3,Image,Python 2.7,Autocomplete,Gtk3,我正在使用一个带有EntryCompletionopobject的Entry,该对象具有ListStore模型。 对于模型中的每个记录,我都希望在自动完成弹出列表中显示一个图像 如何做到这一点? 有没有可能在模型中添加一个Gtk.cellRenderPixBuf列?很有趣,我找不到任何这样的例子,但事实证明这是可能的,也不是非常复杂。让我们从一个目标的小图像开始,它使用图标来证明理由。 那么我们如何实现这一点呢?首先,我们创建一个ListStore,其中包含一个列,其中包含要匹配的字符串和一个

我正在使用一个带有
EntryCompletion
opobject的
Entry
,该对象具有
ListStore
模型。 对于模型中的每个记录,我都希望在自动完成弹出列表中显示一个图像

如何做到这一点?
有没有可能在模型中添加一个
Gtk.cellRenderPixBuf
列?

很有趣,我找不到任何这样的例子,但事实证明这是可能的,也不是非常复杂。让我们从一个目标的小图像开始,它使用图标来证明理由。

那么我们如何实现这一点呢?首先,我们创建一个
ListStore
,其中包含一个列,其中包含要匹配的字符串和一个要转换为pixbuf的图标名(也可以直接转换为pixbuf)

下一步是设置
EntryCompletion
并将其与
Liststore

    # Create the Entry Completion and link it to the list store
    completion = Gtk.EntryCompletion()
    completion.set_model(list_store)
现在神奇的是,我们需要创建两个渲染器,一个用于文本,一个用于pixbufs。然后,我们将这些内容打包到完成项中,以向其中添加列

    # Create renderer's for the pixbufs and text
    image_renderer = Gtk.CellRendererPixbuf.new()
    cell_renderer = Gtk.CellRendererText.new()

    # Pack the columns in to the completion, in this case first the image then the string
    completion.pack_start(image_renderer, True)
    completion.pack_start(cell_renderer, True)
为了确保渲染器使用正确的列,我们在这里指定渲染器应该读取
列表存储
中的哪个列。对于
image\u渲染器
,我们设置
icon\u name
属性作为图标名称。如果我们给它输入
Pixbuf
,我们就需要
Pixbuf

    # Set up the renderer's such that the read the correct column
    completion.add_attribute(image_renderer, "icon_name", 1)
    completion.add_attribute(cell_renderer, "text", 0)
因为没有多列,所以我们需要告诉完成项哪个列包含字符串。在我们的例子中,列0

    # Tell the completion which column contains the strings to base the completion on
    completion.props.text_column = 0

    # Create the entry and link it to the completion
    entry = Gtk.Entry()
    entry.set_completion(completion)

就这样

有趣的是,我找不到这方面的任何例子,但事实证明这是可能的,也不是非常复杂。让我们从一个目标的小图像开始,它使用图标来证明理由。

那么我们如何实现这一点呢?首先,我们创建一个
ListStore
,其中包含一个列,其中包含要匹配的字符串和一个要转换为pixbuf的图标名(也可以直接转换为pixbuf)

下一步是设置
EntryCompletion
并将其与
Liststore

    # Create the Entry Completion and link it to the list store
    completion = Gtk.EntryCompletion()
    completion.set_model(list_store)
现在神奇的是,我们需要创建两个渲染器,一个用于文本,一个用于pixbufs。然后,我们将这些内容打包到完成项中,以向其中添加列

    # Create renderer's for the pixbufs and text
    image_renderer = Gtk.CellRendererPixbuf.new()
    cell_renderer = Gtk.CellRendererText.new()

    # Pack the columns in to the completion, in this case first the image then the string
    completion.pack_start(image_renderer, True)
    completion.pack_start(cell_renderer, True)
为了确保渲染器使用正确的列,我们在这里指定渲染器应该读取
列表存储
中的哪个列。对于
image\u渲染器
,我们设置
icon\u name
属性作为图标名称。如果我们给它输入
Pixbuf
,我们就需要
Pixbuf

    # Set up the renderer's such that the read the correct column
    completion.add_attribute(image_renderer, "icon_name", 1)
    completion.add_attribute(cell_renderer, "text", 0)
因为没有多列,所以我们需要告诉完成项哪个列包含字符串。在我们的例子中,列0

    # Tell the completion which column contains the strings to base the completion on
    completion.props.text_column = 0

    # Create the entry and link it to the completion
    entry = Gtk.Entry()
    entry.set_completion(completion)

就这样

这就是一个例子。很好的发现!当我写下这个答案时,我找不到任何例子:)就是一个例子。很好的发现!当我写下这个答案时,我找不到任何例子:)