Search code examples
python-3.xcomboboxgtkpygtkpygobject

Use Gtk.ComboBox with a Gtk.TreeStore in PyGObject


I would like to have a Gtk.ComboBox with elements displayed like a tree. It means some of the rows should have indention depending on there level in the tree.

When I interprete the documentation correct it should be possible with using a Gtk.TreeStore as data structure (model) behinde the control.

Maybe I am missinterpreting the docu and it is not possible to use Gtk.TreeStore with it?

But it doesn't work in my example. I have experience with Gtk.TreeStore and Gtk.TreeView.

The example code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        # The Model
        store = Gtk.TreeStore(int, str)
        # first item in the row is an internal ID that should not
        # be displayed in the combo box
        it = store.append(parent=None, row=[1, "Eins"])
        it = store.append(parent=it, row=[2, "Zwei"])
        it = store.append(parent=it, row=[3, "Drei"])

        # expected result
        # Eins
        # |- Zwei
        #  |- Drei

        # The View
        combo = Gtk.ComboBox.new_with_model(store)
        renderer = Gtk.CellRendererText()
        combo.pack_start(renderer, False)
        combo.add_attribute(renderer, "text", 1)

        box = Gtk.VBox()
        box.add(combo)
        self.add(box)

if __name__ == '__main__':
    window = MyWindow()
    window.show_all()
    Gtk.main()

A visual example enter image description here


Solution

  • The answer it that it is not possible to use a Gtk.ComboBox with a Gtk.TreeStore like data structure inside.

    There is only the workaround with indentions.