[pygtk] Inserting items into ComboBox

Johannes Bauer dfnsonfsduifb at gmx.de
Wed Mar 4 01:07:28 WST 2009


Walter Leibbrandt schrieb:

> The *_text() methods are only applicable if the ComboBox was created
> with gtk.combo_box_new_text() or gtk.combo_box_entry_new_text(). If the
> ComboBox was created in another way (such as in Glade), you have to
> initialize it yourself:
> 
> def create_combo_with_items(items):
>    combo = gtk.ComboBox()
>    ls = gtk.ListStore(str)
>    for i in items:
>        ls.append([str(i)])
>    combo.set_model(ls)
>    cellr = gtk.CellRendererText()
>    combo.pack_start(cellr)
>    combo.add_attribute(cellr, 'text', 0)
>    return combo

Uhh, that's a whole lot of pain for such a simple thing as changing the
items in a combobox. Especially since I do not only need to create the
combobox, I also need to place it in my window and make it visible. I
really like the idea of having a XML-GUI definition and loading it at
runtime. Why is it such a hassle to perform this easy task? I'd have
thought that something along the lines of

combobox.items = [ "foo", "bar ]

exists (that somewhat resembles the Delphi syntax for handling
comboboxes which I liked very much).

Is there no easier way than manually creating the combobox at runtime?
Can I somehow convince Glade to create a combobox in the variant you
presented?

Thanks for your help,
Kind regards,
Johannes


More information about the pygtk mailing list