RCS/faq16.008.htp,v --> standard output
revision 1.2
Title: 16.8. How do I populate a ComboBox or ComboBoxEntry with strings?
Last-Changed-Date: Mon Nov 29 21:46:17 2004
Last-Changed-Author: Christian Robottom Reis
Last-Changed-Email: kiko@async.com.br
Last-Changed-Remote-Host: manonegra.async.com.br
Last-Changed-Remote-Address: 192.168.99.6
For glade users, you can create a simple model for your ComboBox* using
store = gtk.ListStore(gobject.TYPE_STRING)
store.append (["testing1"])
store.append (["testing2"])
store.append (["testing3"])
store.append (["testing4"])
For a ComboBoxEntry, it's then a matter of assigning the model to the combo and specifying the text column:
combo = tree.get_widget("comboboxentry1")
combo.set_model(store)
combo.set_text_column(0)
For a ComboBox, pack a renderer into it, specifying the text cell:
combo = tree.get_widget("combobox1")
combo.set_model(store)
cell = gtk.CellRendererText()
combo.pack_start(cell, True)
combo.add_attribute(cell, 'text',0)
Thomas Hinkle offers a convenience function that does all this for you:
def set_model_from_list (cb, items):
"""Setup a ComboBox or ComboBoxEntry based on a list of strings."""
model = gtk.ListStore(str)
for i in items:
model.append([i])
cb.set_model(model)
if type(cb) == gtk.ComboBoxEntry:
cb.set_text_column(0)
elif type(cb) == gtk.ComboBox:
cell = gtk.CellRendererText()
cb.pack_start(cell, True)
cb.add_attribute(cell, 'text', 0)
If you are not using glade, it's easier to use the gtk.combo_box_new_text() function with the append_text(), prepend_text(), insert_text() and remove_text() methods as explained here:
http://www.pygtk.org/pygtk2tutorial/ch-NewInPyGTK2.4.html#sec-ComboBox
http://www.pygtk.org/pygtk2reference/class-gtkcombobox.html#function-gtk--combo-box-new-text
(John Finlay, Thomas Hinkle)