[pygtk] Inserting items into ComboBox

Walter Leibbrandt walter at translate.org.za
Wed Mar 4 00:50:30 WST 2009


Hi,

Johannes Bauer wrote:
> Hi list,
>
> I've tried for an hour to insert items into a ComboBox - but just don't
> know how. append_text and its variants (insert_text, modify_text,
> prepend_text) don't seem to do the job as they're (from what I could
> read in the GTK documentation) only available for a specialized
> list-type combobox.
>
> So how do I simply insert items into a combobox during runtime?
>
> Kind regards,
> Johannes
>   
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

See the documentation for the used methods for more information.

HTH

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

Recent blogs:
* Firefox-style button with a pop-up menu
http://www.translate.org.za/blogs/walter/en/content/firefox-style-button-pop-menu
* Virtaal's MVCisation
* Things that changed the way I code




More information about the pygtk mailing list