在不滚动 headers 的情况下滚动 PyGObject 中 TreeView 的内容
Scroll content of a TreeView in PyGObject without scrolling the headers
我想滚动Gtk.TreeView
的内容。这意味着行而不是 header。所以使用 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()
您应该将 TreeView
打包成 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()
TreeView 足够智能,可以只滚动内容:
我想滚动Gtk.TreeView
的内容。这意味着行而不是 header。所以使用 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()
您应该将 TreeView
打包成 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()
TreeView 足够智能,可以只滚动内容: