1

我正在尝试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()
4

1 回答 1

0

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

因此,我创建了另一种方法来捕获列表存储“已更改”信号,并编写了代码以cellrendercombobox在该方法中动态填充列表存储。

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>
于 2012-08-28T13:10:47.260 回答