在不滚动标题的情况下在PyGObject中滚动TreeView的内容

时间:2018-05-07 21:27:58

标签: python python-3.x scroll pygobject

我想滚动Gtk.TreeView内容。这意味着行而不是标题。所以使用Gtk.ScrolledWindow在这里没有用,因为这会滚动完整的小部件。

我找到Gtk.Adjustment但不知道如何使用它。

这是我到目前为止所尝试的

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class TreeView(Gtk.TreeView):
    def __init__(self):
        # model
        self.model = Gtk.ListStore.new([int])
        for i in range(30):
            self.model.append([i])

        # view
        Gtk.TreeView.__init__(self, self.model)

        col_a = Gtk.TreeViewColumn('int',
                                   Gtk.CellRendererText(),
                                   text=0)
        self.append_column(col_a)

        # "adjustment" ???
        self.adj = Gtk.Adjustment()
        self.set_vadjustment(self.adj)


class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title='Mein Gtk-Fenster')
        self.set_default_size(100, 120)

        self.view = TreeView()

        # layout
        self.layout = Gtk.Grid()
        self.add(self.layout)
        self.layout.attach(self.view, 0, 1, 1, 1)

        self.connect('destroy', Gtk.main_quit)
        self.show_all()

if __name__ == '__main__':
    win = Window()
    Gtk.main()

1 个答案:

答案 0 :(得分:1)

您应该将Gtk.ScrolledWindow打包到class Window(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title='Mein Gtk-Fenster') self.set_default_size(100, 120) self.view = TreeView() scroll = Gtk.ScrolledWindow () # 1 scroll.add (self.view) # 2 scroll.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) self.add (scroll) self.connect('destroy', Gtk.main_quit) self.show_all()

{{1}}

TreeView非常智能,只能滚动内容:

  

scrolled treeview example