0

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

所以这是代码

TreeView 和 ListStore:labelDB = gtk.Label('Database')

    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 始终为空

我希望有人可以帮助我,请

4

1 回答 1

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()
于 2013-06-26T11:49:18.590 回答