python - 为什么 PyGtk 中的 set_model 方法将第一列的值复制到第三列?

标签 python user-interface interface treeview pygtk

我正在使用 PyGtk 在 TreeView 中显示一些字符串信息。 这是我的代码:

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)

然后,我创建了一个方法来填充事件按钮后的 TreeView 。调用:

 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)

以及方法(我在 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)

当我在 TreeView 中看到结果时,我在第三列得到与第一列相同的值,当然它们是不同的。图片如下: table

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

最佳答案

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

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

正确的方法。

关于python - 为什么 PyGtk 中的 set_model 方法将第一列的值复制到第三列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20528152/

相关文章:

delphi - 具有透明 PNG 图像和发光悬停效果的 TButton

actionscript-3 - AS3 如何使变量只包含实现给定接口(interface)的对象

Android 如何判断手指松了

python pygame 输入控件

java - 如何在 Java 中将 ActionListener 添加到 JButton 上

ios - 为ios制作自定义图标时的困惑

c# - 为每个类提取一个接口(interface)是最佳实践吗?

python - 如何从图像 url (jpeg) 读取字节并在 Python 2.7 上以 base64 编码?

python - 即使重新安装 matplotlib、libpng 和 brew 后,导入 pylab 也会失败

python - import 是否调用 __new__ 静态方法?