如何在Python中创建带有图像的GTK ComboBox?

时间:2013-04-04 09:37:12

标签: python gtk pygobject

如何创建一个显示条目列表的ComboBox,每个条目包含一些文本和一个图标?

我正在使用Python和GTK3以及GObject内省。

2 个答案:

答案 0 :(得分:6)

这是一个如何做到这一点的例子,灵感来自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()

答案 1 :(得分:2)

GTK2中的相同示例,受您的代码启发:

import pygtk
pygtk.require('2.0')
import gtk
import gtk.gdk
import gobject
import gc

store = gtk.ListStore(str, gtk.gdk.Pixbuf) 

pb = gtk.gdk.pixbuf_new_from_file("picture.png")
store.append(["Test", pb])

combo = gtk.ComboBox(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(gtk.WINDOW_TOPLEVEL)
window.add(combo)
window.show_all()

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

gtk.main()