43.4. CellRendererCombo

Using the CellRendererCombo, a ComboBox-style and ComboBoxEntry-style object can be rendered into a TreeView. This allows selection of items from a list within a TreeView.

Constructor

cellrenderercombo = gtk.CellRendererCombo()

Methods

After construction of the CellRendererCombo, a data store needs to be attached.

cellrenderercombo.set_property("model", model)

The model parameter is the name of a ListStore which will be used to hold data for the Combo.

We also need to specify which column within the ListStore will be accessed by the Combo.

cellrenderercombo.set_property("column", column)

The column parameter is the number of the column within the ListStore. Remember that the first column number is 0.

A CellRendererCombo can operate in two modes. It can be used with and without an associated Entry widget.

cellrenderercombo.set_property("has-entry", entry)

When the entry property is set to False, the CellRendererCombo can not accept any textual input from the user.

Example

#!/usr/bin/env python

import gtk

class CellRendererCombo:
    def __init__(self):
        window = gtk.Window()
        window.set_default_size(200, 200)
        
        liststore_manufacturers = gtk.ListStore(str)
        manufacturers = ["Sony", "LG", "Panasonic", "Toshiba", "Nokia", "Samsung"]
        for item in manufacturers:
            liststore_manufacturers.append([item])
        
        liststore_hardware = gtk.ListStore(str, str)
        liststore_hardware.append(["Television", "Samsung"])
        liststore_hardware.append(["Mobile Phone", "LG"])
        liststore_hardware.append(["DVD Player", "Sony"])
        
        treeview = gtk.TreeView(liststore_hardware)
        column_text = gtk.TreeViewColumn("Text")
        column_combo = gtk.TreeViewColumn("Combo")
        treeview.append_column(column_text)
        treeview.append_column(column_combo)
        
        cellrenderer_text = gtk.CellRendererText()
        column_text.pack_start(cellrenderer_text, False)
        column_text.add_attribute(cellrenderer_text, "text", 0)
        
        cellrenderer_combo = gtk.CellRendererCombo()
        cellrenderer_combo.set_property("editable", True)
        cellrenderer_combo.set_property("model", liststore_manufacturers)
        cellrenderer_combo.set_property("text-column", 0)
        column_combo.pack_start(cellrenderer_combo, False)
        column_combo.add_attribute(cellrenderer_combo, "text", 1)
        
        window.connect("destroy", lambda w: gtk.main_quit())
        cellrenderer_combo.connect("edited", self.combo_changed, liststore_hardware)

        window.add(treeview)
        window.show_all()
    
    def combo_changed(self, widget, path, text, model):
        model[path][1] = text
        
CellRendererCombo()
gtk.main()

Download cellrenderercombo.py