Search code examples
pythongtkpygobject

How can I create a GTK ComboBox with images in Python?


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.


Solution

  • 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()