tags:

views:

1210

answers:

2

I'm using Glade 3 to create a GtkBuilder file for a PyGTK app I'm working on. It's for managing bandwidth, so I have a gtk.ComboBox for selecting the network interface to track.

How do I add strings to the ComboBox at runtime? This is what I have so far:

self.tracked_interface = builder.get_object("tracked_interface")

self.iface_list_store = gtk.ListStore(gobject.TYPE_STRING)
self.iface_list_store.append(["hello, "])
self.iface_list_store.append(["world."])
self.tracked_interface.set_model(self.iface_list_store)
self.tracked_interface.set_active(0)

But the ComboBox remains empty. I tried RTFM'ing, but just came away more confused, if anything.

Cheers.

+1  A: 

Hey, I actually get to answer my own question!

You have to add gtk.CellRendererText into there for it to actually render:

self.iface_list_store = gtk.ListStore(gobject.TYPE_STRING)
self.iface_list_store.append(["hello, "])
self.iface_list_store.append(["world."])
self.tracked_interface.set_model(self.iface_list_store)
self.tracked_interface.set_active(0)
# And here's the new stuff:
cell = gtk.CellRendererText()
self.tracked_interface.pack_start(cell, True)
self.tracked_interface.(cell, "text", 0)

Retrieved from, of course, the PyGTK FAQ.

Bernard
+2  A: 

Or you could just create and insert the combo box yourself using gtk.combo_box_new_text(). Then you'll be able to use gtk shortcuts to append, insert, prepend and remove text.

combo = gtk.combo_box_new_text()
combo.append_text('hello')
combo.append_text('world')
combo.set_active(0)

box = builder.get_object('some-box')
box.pack_start(combo, False, False)
Ivan Baldin
Huh, neat. And another sentence, suitable for padding out this comment to a SO acceptable length.
Bernard