GTK组合框:添加带有两个字段的项



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

下面是我的代码:
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)

谢谢

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

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

最新更新