[pygtk] Looking for help with comboboxes

Neil Muller drnlmuller+gtk at gmail.com
Wed Apr 15 00:01:09 WST 2009


On Tue, Apr 14, 2009 at 4:39 PM, Gerald Britton
<gerald.britton at gmail.com> wrote:
> Would someone be able to point out what I am missing?
>
> glade = """
> <?xml version="1.0"?>
> <interface>
>    <child>
>      <object class="GtkComboBox" id="combobox">
>        <property name="visible">True</property>
>        <property name="model">liststore</property>

The combobox needs to contain a CellRenderer, and the CellRenderer
needs to be linked to the model.

The pure gtkbuilder approach would be to add:

       <child>
          <object class="GtkCellRendererText" id="combocell" />
          <attributes>
             <attribute name="text">0</attribute>
          </attributes>
       </child>

to the combobox definition.

>      </object>
>    </child>

> w = g.get_object('window')
> c = g.get_object('combobox')
> l = g.get_object('liststore')

Or you could add:

cell = gtk.CellRendererText()
c.pack_start(cell)
c.add_attribute(cell, 'text', 0)

> for i in range(10):
>  c.append_text("line %d" % i)  # 1. try to append the text to the combobox
>  l.append(["line %d" % i])        # 2. try to add the text to the liststore in the combobox.

If using a CellRendererText that uses the first column of the list
store, both methods should work, IIRC.

append_text is designed to be used with ComboBox's created with
gtk.combo_box_new_text, though, and needs the ComboBox to match it's
assumptions, which can be problematic, and makes changing things later
potentially harder, so appending to the list store is probably the
better choice here.

-- 
Neil Muller
drnlmuller at gmail.com

I've got a gmail account. Why haven't I become cool?


More information about the pygtk mailing list