2013-12-11 23 views
1

我正在使用PyGtk在树形视图中显示一些字符串信息。 这里去我的代码:为什么PyGtk中的set_model方法重复第三列第一列的值?

def create_table(self): 
    self.mainbox = gtk.ScrolledWindow() 
    self.mainbox.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 
    self.window.add(self.mainbox) 

    model = gtk.ListStore(str, str, str) 
    self.treeview = gtk.TreeView(model=None) 

    col = gtk.TreeViewColumn("Element") 
    self.treeview.append_column(col) 
    cell = gtk.CellRendererText() 
    col.pack_start(cell, expand=False) 
    col.set_attributes(cell, text=0) 

    col = gtk.TreeViewColumn("Test") 
    self.treeview.append_column(col) 
    cell = gtk.CellRendererSpin() 
    col.pack_start(cell, expand=False) 
    col.set_attributes(cell, text=1) 

    col = gtk.TreeViewColumn("Command") 
    self.treeview.append_column(col) 
    cell = gtk.CellRendererSpin() 
    col.pack_start(cell, expand=False) 
    col.set_attributes(cell, text=0) 

    cell = gtk.CellRendererCombo() 
    self.mainbox.add(self.treeview) 
    self.mainbox.set_size_request(500, 260) 
    self.mainbox.show() 
    self.vbox.pack_start(self.mainbox, expand=False, fill=True, padding=0) 

然后,我创建了一个方法的事件按钮后,填补了树视图。呼叫:

def populate_treeview_button(self): 
    button = gtk.Button(label='Populate Table') 
    button.connect("clicked", self.create_model) 
    self.vbox.pack_start(button, expand=False, fill=True, padding=0) 

并且所述方法(I接收在table_information属性类型的字典的一个列表,其中所述键是一个元素(串)并将该值与2串的列表):

def create_model(self, beats_me_param): 
    model = gtk.ListStore(str, str, str) 

    elements = [] 
    tests = [] 
    commands = [] 

    table_information = self.get_organized_table() 

    for i in table_information: 
     for dicts in i: 
      for element in dicts.keys(): 
       elements.append(element) 

    for i in table_information: 
     for dicts in i: 
      for value in dicts.values(): 
       tests.append(value[0]) 
       commands.append(value[1]) 


    for i in range(len(elements)): 
     model.append([elements[i], tests[i], commands[i]])    

    self.treeview.set_model(model) 

当我在我的树形视图中看到了结果,我在第三列获得了第一列的相同值,当然它们是不同的。图像波纹管: table

我改变了“附加”时刻的元素顺序,发生了同样的情况,值发生了变化,但是在第三列重复更改的值。出了什么问题?

回答

1

问题是因为我设置了相同的索引到第三和第一列。

col = gtk.TreeViewColumn("Command") 
self.treeview.append_column(col) 
cell = gtk.CellRendererSpin() 
col.pack_start(cell, expand=False) 
col.set_attributes(cell, text=2) 

正确的方法。

相关问题