简体   繁体   中英

PyGtk CellRendererCombo using Pixbuf

I would like to display a ComboBox in my TreeView that contains some Icons. So I created a ListStore to hold the data.

    # Initialize a list store for the combobox.
    priorityModel = Gtk.ListStore(GObject.TYPE_INT, GdkPixbuf.Pixbuf)
    priorityModel.append([0, self.loadPixbuf('./data/media/flag_blue.png')])
    priorityModel.append([1, self.loadPixbuf('./data/media/flag_green.png')])
    priorityModel.append([2, self.loadPixbuf('./data/media/flag_yellow.png')])
    priorityModel.append([3, self.loadPixbuf('./data/media/flag_red.png')])

Then I created the Gtk.CellRendererCombo object and assigned the above Gtk.ListStore as model.

    # Setup the priority cell renderer, ...
    self.priorityRenderer = Gtk.CellRendererCombo()
    self.priorityRenderer.set_property( 'editable', True )
    self.priorityRenderer.set_property("model", priorityModel)
    self.priorityRenderer.connect("edited", self.on_priority_changed, self.listStore, 3)

Finally I created a new Gtk.TreeViewColumn and assigned the CellRenderer and added it to the TreeView.

    # ... setup the priority column ...
    self.colPriority = Gtk.TreeViewColumn("Priority", self.priorityRenderer, text=3 )

    # ... and add it to the treeview.
    self.append_column( self.colPriority )

All nice but how could I display the Pixbuf icon instead of the integer and how do I initialize the ComboBox using the integer? And, second question, is it possible to show only the icon when the combobox is not active and show the icon and some text when the value is changed (ComboBox active)?

You need a Gtk.CellRendererPixbuf to display the icon. See for example the excellent Python 3 Gtk+ Tutorial .

Answering your second question: Yes. Just connect to the "changed" signal of the Gtk.CellRendererCombo and add/remove the cell renderer dynamically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM