CellRendererCombo

A CellRendererCombo widget provides a dropdown menu within a TreeView. The functionality is similar to that provide by a ComboBox.

Note

See the CellRenderer page for additional methods available.

Constructor

The CellRendererCombo can be constructed using the following:

cellrenderercombo = Gtk.CellRendererCombo()

Properties

The configuration of the CellRendererCombo is made using the property functions:

cellrenderercombo.set_property("item", value)

The property items available for use with the CellRendererCombo are:

  • "has-entry" - setting to True enables text to be entered by the user along with selecting a value from a menu.
  • "model" - specifies`the data store which will be used to retrieve values from. This should be set to the name of an appropriate ListStore or TreeStore containing the values.
  • "text-column" - used to set the column from which the values are to be retrieved.

Signals

The common signals of the CellRendererCombo are:

"changed" (cellrenderercombo, path, treeiter)

The "changed" signal emits when the user changes which item has been selected. The path value is a string which identifies the cell relative to the tree view model. The treeiter is the iter of the item selected relative to the combo box model. These items can be put together to return the item from the model.

Example

Below is an example of a CellRendererCombo:

#!/usr/bin/env python

from gi.repository import Gtk

class CellRendererCombo(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, 200)
        self.connect("destroy", Gtk.main_quit)

        self.liststoreAppliance = Gtk.ListStore(str, str)
        self.liststoreAppliance.append(["Dishwasher", "Bosch"])
        self.liststoreAppliance.append(["Refrigerator", "Samsung"])
        self.liststoreAppliance.append(["Cooker", "Hotpoint"])

        self.liststoreManufacturers = Gtk.ListStore(str)
        self.liststoreManufacturers.append(["Bosch"])
        self.liststoreManufacturers.append(["Whirlpool"])
        self.liststoreManufacturers.append(["Hotpoint"])
        self.liststoreManufacturers.append(["DeLonghi"])
        self.liststoreManufacturers.append(["Samsung"])

        treeview = Gtk.TreeView()
        treeview.set_model(self.liststoreAppliance)
        self.add(treeview)

        cellrenderertext = Gtk.CellRendererText()

        treeviewcolumn = Gtk.TreeViewColumn("Appliance")
        treeview.append_column(treeviewcolumn)
        treeviewcolumn.pack_start(cellrenderertext, False)
        treeviewcolumn.add_attribute(cellrenderertext, "text", 0)

        cellrenderercombo = Gtk.CellRendererCombo()
        cellrenderercombo.set_property("editable", True)
        cellrenderercombo.set_property("model", self.liststoreManufacturers)
        cellrenderercombo.set_property("text-column", 0)
        cellrenderercombo.connect("changed", self.on_combo_changed)

        treeviewcolumn = Gtk.TreeViewColumn("Manufacturer")
        treeview.append_column(treeviewcolumn)
        treeviewcolumn.pack_start(cellrenderercombo, False)
        treeviewcolumn.add_attribute(cellrenderercombo, "text", 1)

    def on_combo_changed(self, cellrenderercombo, treepath, treeiter):
        self.liststoreAppliance[treepath][1] = self.liststoreManufacturers[treeiter][0]

window = CellRendererCombo()
window.show_all()

Gtk.main()

Download: CellRendererCombo