我如何在Python中创建一个包含图像的GTK组合框? [英] How can I create a GTK ComboBox with images in Python?

查看:107
本文介绍了我如何在Python中创建一个包含图像的GTK组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用Python和GTK3与GObject内省。

解决方案

下面是一个如何做到这一点的例子,受 C 的这个答案。

  from gi.repository import Gtk 
from gi.repository import GdkPixbuf

store = Gtk.ListStore(str,GdkPixbuf.Pixbuf)

pb = GdkPixbuf.Pixbuf.new_from_file_at_size(picture.png,32,32 )
store.append([Test,pb])

combo = Gtk.ComboBox.new_with_model(store)

renderer = Gtk.CellRendererText()
combo.pack_start(renderer,True)
combo.pack_start(渲染器,text,0)

渲染器= Gtk.CellRendererPixbuf()
combo.pack_start (渲染器,False)
combo.add_attribute(渲染器,pixbuf,1)

window = Gtk.Window()
window.add(组合)
无线ndow.show_all()

window.connect('delete-event',lambda w,e:Gtk.main_quit())

Gtk.main()


How can I create a ComboBox that displays a list of entries, each containing some text and an icon?

I'm using Python and GTK3 with GObject introspection.

解决方案

Here's an example of how to do that, inspired by this answer for C.

from gi.repository import Gtk
from gi.repository import GdkPixbuf

store = Gtk.ListStore(str, GdkPixbuf.Pixbuf)

pb = GdkPixbuf.Pixbuf.new_from_file_at_size("picture.png", 32, 32)
store.append(["Test", pb])

combo = Gtk.ComboBox.new_with_model(store)

renderer = Gtk.CellRendererText()
combo.pack_start(renderer, True)
combo.add_attribute(renderer, "text", 0)

renderer = Gtk.CellRendererPixbuf()
combo.pack_start(renderer, False)
combo.add_attribute(renderer, "pixbuf", 1)

window = Gtk.Window()
window.add(combo)
window.show_all()

window.connect('delete-event', lambda w, e: Gtk.main_quit())

Gtk.main()

这篇关于我如何在Python中创建一个包含图像的GTK组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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