[pygtk] Inserting items into ComboBox
Timo
timomlists at gmail.com
Wed Mar 4 02:08:51 WST 2009
Johannes Bauer schreef:
> 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?
>
I also always did it the way Walter described. Until I recently found
out that if you enter something as an item in Glade (one character is
enough), and then remove it again so you keep an empty combobox. Then
this piece of code is enough:
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.
> Thanks for your help,
> Kind regards,
> Johannes
> _______________________________________________
> pygtk mailing list pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/
>
More information about the pygtk
mailing list