Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add items to a gtk.ComboBox created through glade at runtime?

Tags:

python

pygtk

gtk

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.

like image 231
Bernard Avatar asked Jul 24 '09 10:07

Bernard


2 Answers

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)
like image 169
Ivan Baldin Avatar answered Nov 16 '22 00:11

Ivan Baldin


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.add_attribute(cell, "text", 0)

Retrieved from, of course, the PyGTK FAQ.

Corrected example thanks to Joe McBride

like image 30
Bernard Avatar answered Nov 16 '22 02:11

Bernard