将项目动态添加到Liststore中的CellRendererCombo:Pygtk

时间:2012-08-23 09:40:06

标签: gtk pygtk

我正在尝试动态地向CellRendererCombo添加项目。请参阅下面的代码。当我点击'Samsung'时,组合框中会列出相应的项目。现在的问题是,当我点击'LG'时,会列出与“三星”对应的项目。简而言之,组合框显示以前选择的键的项目,而不是当前键。

我该如何解决这个问题?

#!/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()

1 个答案:

答案 0 :(得分:0)

问题解决了。问题是因为弹出的组合框上的模型是在发出GtkCellEditable::editing-started信号之前设置的。 editing-started处理程序已设置为combobox中的下一个弹出窗口。

所以我创建另一个方法来捕获liststore“changed”信号,并编写代码以在该方法中动态填充cellrendercombobox的liststore。

treeviewmodel = self.tree_view.get_model()
tree_selection = self.tree_view.get_selection()
tree_selection.connect("changed", self.onSelectionChanged)
......
def onSelectionChanged(self,tree_selection) :
    value=None
    (model, pathlist) = tree_selection.get_selected_rows()
    for path in pathlist :
        tree_iter = model.get_iter(path)
        value = model.get_value(tree_iter,0)
        print value
    if value:
        <code to populate the liststore>