[pygtk] Inserting items into ComboBox

Walter Leibbrandt walter at translate.org.za
Sat Mar 7 00:31:36 WST 2009

Johannes Bauer wrote:
> Timo schrieb:
>> for item in ["foo", "bar"]:
>>    combobox.get_model().append([item])
>> Apparently after entering something in Glade, the liststore etc are
>> generated. And by removing these items, it keeps these.
> Sadly, my version of Glade does not generate the XML the way you
> described when I do it the way you described :-(
> I guess I'll stick with Walter's solution then. It's just very very
> confusing for a GTK beginner to have to do such low-level tasks in order
> to perform such a relatively simple and common thing.
> Kind regards and thanks for your help,
> Johannes
And so I learn something new! On a hunch, I compared a Glade file with 
an empty (unaltered) combo box and a combo box with its "Items" field 
"changed". I put the "changed" in quotes, because what I did was add a 
character and delete it again, so no text was really added. It turns out 
that the output XML differs by one line that defines an "items" property 
(see attached comboboxes.diff).

The inclusion of that "items" property causes the created ComboBox to be 
initialized (ListStore and CellRendererText created and correctly 
associated with the ComboBox). In fact you can use the *_text() methods 
with such an "initialized" ComboBox!

I've attached my test program and Glade file as well.

This is certainly a new discovery for me and should make it much simpler 
to work with simple ComboBoxes.


Walter Leibbrandt                  http://translate.org.za/blogs/walter
Software Developer                                  +27 12 460 1095 (w)

Recent blogs:
* Firefox-style button with a pop-up menu
* Virtaal's MVCisation
* Things that changed the way I code

-------------- next part --------------
A non-text attachment was scrubbed...
Name: comboboxes.diff
Type: text/x-patch
Size: 667 bytes
Desc: not available
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20090306/8483bd58/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: combo_with_items.glade
Type: application/x-glade
Size: 461 bytes
Desc: not available
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20090306/8483bd58/attachment-0001.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: combo_test.py
Type: text/x-python
Size: 617 bytes
Desc: not available
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20090306/8483bd58/attachment.py 

More information about the pygtk mailing list