我正在尝试CellRendererCombo
动态添加项目。请参阅下面的代码。当我点击'Samsung'
相应的项目时,会在组合框中列出。现在的问题是,当我单击 时'LG'
,列出了与“Samsung”相对应的项目。简而言之,组合框显示的是先前选择的键的项目,而不是当前键的项目。
我该如何解决这个问题?
#!/usr/bin/env python
import gtk,os
class CellRendererCombo:
def __init__(self):
window = gtk.Window()
window.set_default_size(200, 200)
liststore_hardware = gtk.ListStore(str, str)
liststore_hardware.append(["Television", "Samsung"])
liststore_hardware.append(["Mobile Phone", "LG"])
liststore_hardware.append(["DVD Player", "Sony"])
treeview = gtk.TreeView(liststore_hardware)
column_text = gtk.TreeViewColumn("Text")
column_combo = gtk.TreeViewColumn("Combo")
treeview.append_column(column_text)
treeview.append_column(column_combo)
cellrenderer_text = gtk.CellRendererText()
column_text.pack_start(cellrenderer_text, False)
column_text.add_attribute(cellrenderer_text, "text", 0)
self.cellrenderer_combo = gtk.CellRendererCombo()
self.cellrenderer_combo.set_property("editable", True)
self.cellrenderer_combo.set_property("text-column", 0)
column_combo.pack_start(self.cellrenderer_combo, False)
column_combo.add_attribute(self.cellrenderer_combo, "text", 1)
window.connect("destroy", lambda w: gtk.main_quit())
self.cellrenderer_combo.connect("editing-started", self.populate_combo)
self.cellrenderer_combo.connect("edited", self.combo_changed, liststore_hardware)
window.add(treeview)
window.show_all()
def combo_changed(self, widget, path, text, model):
model[path][1] = text
def populate_combo(self,*args):
liststore_manufacturers = gtk.ListStore(str)
manufacturers={}
manufacturers['Samsung'] = ["Samsung", "Item1","Item2", "Item3", "Item4"]
manufacturers['LG'] = ["LG", "Item5", "Item6", "Item7", "Item8", "Item9",'Item10']
manufacturers['Sony'] = ["Sony", "Item11", "Item12", "Item13", "Item14", "Item15",'Item16']
catgry=self.cellrenderer_combo.get_property ("text") # read the content of combobox
for item in manufacturers[catgry]:
liststore_manufacturers.append([item])
self.cellrenderer_combo.set_property("model", liststore_manufacturers)
#
CellRendererCombo()
gtk.main()