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)
就这样 这就是一个例子。很好的发现!当我写下这个答案时,我找不到任何例子:)就是一个例子。很好的发现!当我写下这个答案时,我找不到任何例子:)