本文介绍了GTK TreeView:20行最小脚本不会在窗口中显示TreeView本身:怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个最简单的TreeView示例,大部分是从 basictreeview.py 复制而来的,但更为简单,但是TreeView本身不会显示在顶层窗口中.怎么了?

I've got a simplest TreeView example possible, mostly copied from basictreeview.py, but even simplier, but the TreeView itself won't show inside the toplevel window. What's wrong:

import gtk

class Application(object):
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Alignment Editor")
        self.window.set_default_size(1024, 768)
        self.window.connect("delete_event", lambda w,e: gtk.main_quit())

        self.row = ["a", "b", "c", "d", "e", "f", "g", "h"]
        self.store = gtk.ListStore(*[str]*len(self.row)) #create len(word) columns
        self.store.append(self.row)

        self.treeview = gtk.TreeView(self.store)
        self.treeview.set_reorderable(True)
        self.window.add(self.treeview)
        self.window.show_all()

if __name__ == "__main__":
    Application()
    gtk.main()

推荐答案

您缺少TreeviewColumns和CellRenderers,我相信最小的示例是(抱歉GTK3,但与Pygtk相同)

You are missing TreeviewColumns and CellRenderers, I believe the minimal example is (sorry is GTK3 but it's the same as Pygtk)

from gi.repository import Gtk

class Application(object):
    def __init__(self):
        self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        self.window.set_title("Alignment Editor")
        self.window.set_default_size(200, 150)
        self.window.connect("delete_event", lambda w,e: Gtk.main_quit())

        self.row = ["a", "b"]
        self.store = Gtk.ListStore(*[str]*len(self.row)) #create len(word) columns
        self.store.append(self.row)

        cra = Gtk.CellRendererText()
        twcolumna = Gtk.TreeViewColumn("Column a")
        twcolumna.pack_start(cra, True)
        twcolumna.add_attribute(cra, 'text', 0)

        crb = Gtk.CellRendererText()
        twcolumnb = Gtk.TreeViewColumn("Column b")
        twcolumnb.pack_start(crb, True)
        twcolumnb.add_attribute(crb, 'text', 1)

        self.treeview = Gtk.TreeView(self.store)

        self.treeview.append_column(twcolumna)
        self.treeview.append_column(twcolumnb)

        self.treeview.set_reorderable(True)
        self.window.add(self.treeview)
        self.window.show_all()

if __name__ == "__main__":
    Application()
    Gtk.main()

这篇关于GTK TreeView:20行最小脚本不会在窗口中显示TreeView本身:怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 07:58