gtk combobox:添加两个字段的项目

时间:2014-05-22 11:26:19

标签: python combobox gtk pygtk

我想用两个字段向组合框小部件添加项目,因此当用户选择一个项目时,他将看到一个字段,程序将看到两个已选择的字段。

这是我的代码:

slist = gtk.ListStore(str, str)
slist.append(['item_name1', 'item_id1'])
slist.append(['item_name2', 'item_id2'])
slist.append(['item_name3', 'item_id3'])

self.combobox = gtk.ComboBox(model=slist)

cell = gtk.CellRendererText()
self.combobox.pack_start(cell)
self.combobox.add_attribute(cell, 'text', 1)
self.combobox.set_model(slist)

由于

1 个答案:

答案 0 :(得分:2)

示例代码段已经设置了一个包含两列数据的模型,而视图只显示一列。要从类似选择的内容中检索两列数据,您可以使用" get_active_iter()"结合"改变"发出信号以取出整行数据:

def on_selection_changed(combo):
    itr = combo.get_active_iter()
    print(slist.get_value(itr, 0), slist.get_value(itr, 1))

combobox.connect('changed', on_selection_changed)

另请参阅:http://pygtk.org/docs/pygtk/class-gtkcombobox.html#method-gtkcombobox--get-active-iter