Reputation: 13
How could I set up the text of a combobox without knowing its id? I have a combobox populated with a list of name ('Jack','Emily','Paul',...). By default, the combo is set on -1 but I want it to be set on "Paul".
Here is my code to declare and populate the combo with tuple (id,fabName) :
self.cmbFabricant = builder.get_object("cmbFabricant")
self.cmbFabricant.set_model(lstStore)
self.cmbFabricant.set_entry_text_column(1)
Now, I'd like to set the combobox on the item called "Paul". I thought I could write :
self.cmbFabricant.set_active_id('Paul')
I was wrong.
Upvotes: 1
Views: 1245
Reputation: 9663
I could be wrong, but I think set_active_id
is new in GTK+ 3, and PyGTK is GTK+ 2. If you want to use GTK+ 3, you have to switch to PyGObject.
But if you're stuck on PyGTK, you can easily work around it by doing something like this:
import gtk
def set_active_name(combobox, col, name):
liststore = combobox.get_model()
for i in xrange(len(liststore)):
row = liststore[i]
if row[col] == name:
combobox.set_active(i)
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
liststore = gtk.ListStore(int, str)
liststore.append([0, 'Jack'])
liststore.append([1, 'Emily'])
liststore.append([2, 'Paul'])
combobox = gtk.ComboBox()
cell = gtk.CellRendererText()
combobox.pack_start(cell)
combobox.add_attribute(cell, 'text', 1)
combobox.set_model(liststore)
set_active_name(combobox, 1, 'Paul')
window.add(combobox)
window.show_all()
gtk.main()
I'm not sure if there is a more elegant/efficient way, but this works at least.
Upvotes: 1