Matheus Saraiva
Matheus Saraiva

Reputation: 1254

ComboBox with two columns

I'm trying to create a GtkComboBox with two columns. I picked up some examples and I'm trying to adapt but without success. Only the items that would be the second column are shown.

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ComboBox(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("ComboBox")
        self.set_default_size(150, -1)
        self.connect("destroy", Gtk.main_quit)

        slist = Gtk.ListStore(str, str)
        slist.append(['01', 'Ferro'])
        slist.append(['07', 'Uranio'])
        slist.append(['08', 'Cobalto'])

        combobox = Gtk.ComboBox()
        combobox.set_model(slist)
        combobox.set_active(0)
        combobox.set_wrap_width(2)
        self.add(combobox)

        cellrenderertext = Gtk.CellRendererText()
        combobox.pack_start(cellrenderertext, True)
        combobox.add_attribute(cellrenderertext, "text", 1)

window = ComboBox()
window.show_all()

Gtk.main()

I want create a GtkComboBox like this:

enter image description here

Upvotes: 1

Views: 1129

Answers (1)

Matheus Saraiva
Matheus Saraiva

Reputation: 1254

There must be a CellRendererText for each column to be displayed.

cell1 = Gtk.CellRendererText()
cell2 = Gtk.CellRendererText()
combobox.pack_start(cell1, True)
combobox.pack_start(cell2, True)
combobox.add_attribute(cell1, "text", 0)
combobox.add_attribute(cell2, "text", 1)

Upvotes: 1

Related Questions