python gtk在TreeView中填充ListStore

时间:2013-06-26 09:04:11

标签: python treeview gtk

嘿,我是新人,我有一个问题 我正在尝试使用ListStore创建一个TreeView。 ListStore应该从另一个类中获取一个列表,但似乎我无法填充我的ListStore。

所以这是代码

TreeView和ListStore:         labelDB = gtk.Label('数据库')

    self.storeDB = gtk.ListStore(str, str, int, str)

    rentexDB = gtk.CellRendererText()
    colDB1 = gtk.TreeViewColumn('Name', rentexDB)
    colDB2 = gtk.TreeViewColumn('URL', rentexDB)
    colDB3 = gtk.TreeViewColumn('Bitrate', rentexDB)
    colDB4 = gtk.TreeViewColumn('Format', rentexDB)

    self.fillStore()

    pageDB = gtk.TreeView(model=self.storeDB)
    pageDB.append_column(colDB1)
    pageDB.append_column(colDB2)
    pageDB.append_column(colDB3)
    pageDB.append_column(colDB4)

    showData.append_page(pageDB, labelDB)

和fillStore:

def fillStore(self):
    dataInStore = self.record.useDB()

    if dataInStore is not None:
        self.storeDB.clear()
        for row in dataInStore:
            #print row
            self.storeDB.append(row)

    return self.storeDB

当我打印行时,我可以看到我要存储在ListStore中的列表 但ListStore总是空的

我希望有人可以帮助我,

1 个答案:

答案 0 :(得分:1)

下面是一个演示应用程序:

#!/usr/bin/env python3

from gi.repository import Gtk


class Demo:
    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_default_size(640, 480)
        self.window.connect('delete-event', self.on_app_exit)

        scrolledwindow = Gtk.ScrolledWindow()
        self.window.add(scrolledwindow)

        self.liststore = Gtk.ListStore(str, str, int, str)

        # You need to use self.fillStore() instead.
        self.liststore.append(['Debian', 'i386', 0, 'ok'])
        self.liststore.append(['Fedora', 'amd64', 1, 'ok'])
        self.liststore.append(['Ubuntu', 'i386', 2, 'ok'])

        treeview = Gtk.TreeView(model=self.liststore)
        scrolledwindow.add(treeview)

        names = ('Name', 'URL', 'Bitrate', 'Format')
        for i in range(4):
            renderer_text = Gtk.CellRendererText()
            column = Gtk.TreeViewColumn(names[i], renderer_text, text=i)
            treeview.append_column(column)

    def fillStore(self):
        pass

    def run(self):
        self.window.show_all()
        Gtk.main()

    def on_app_exit(self, widget, event=None):
        Gtk.main_quit()


if __name__ == '__main__':
    demo = Demo()
    demo.run()