我遇到Gtk.Treeview
ind Gtk.ScrolledWindow
的问题。我有大量的数据,它不适合我的普通窗口几何,所以我把我的Treeview放在ScrolledWindow
里面,这似乎是正常的方式。这个解决方案的问题是,当我滚动时,我的列标题消失了。这种行为对我来说似乎有点好,因为ScrolledWindow
应该关心它里面的小部件。
那么,如何让我的Treeview可滚动 AND 显示列标题始终位于第一行?
我修改了 basictreeview.py 示例来表示我的问题:
#!/usr/bin/env python
# example basictreeview.py
import pygtk
pygtk.require('2.0')
import gtk
class BasicTreeViewExample:
# close the window and quit
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def __init__(self):
# Create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Basic TreeView Example")
self.window.set_size_request(200, 200)
self.window.connect("delete_event", self.delete_event)
# create a TreeStore with one string column to use as the model
self.liststore = gtk.ListStore(str)
# we'll add some data now - 4 rows with 3 child rows each
for i in range(10):
self.liststore.append([str(i)])
# create the TreeView using treestore
self.treeview = gtk.TreeView(self.liststore)
# create the TreeViewColumn to display the data
self.tvcolumn = gtk.TreeViewColumn("Column-Name", gtk.CellRendererText(), text=0)
# add tvcolumn to treeview
self.treeview.append_column(self.tvcolumn)
# make it searchable
self.treeview.set_search_column(0)
# Allow sorting on the column
self.tvcolumn.set_sort_column_id(0)
# Allow drag and drop reordering of rows
self.treeview.set_reorderable(True)
# Add Treeview to a ScrolledWindow
self.scrolledwindow = gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
self.scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.scrolledwindow.add_with_viewport(self.treeview)
self.window.add(self.scrolledwindow)
self.window.show_all()
def main():
gtk.main()
if __name__ == "__main__":
tvexample = BasicTreeViewExample()
main()
答案 0 :(得分:11)
将self.scrolledwindow.add_with_viewport
更改为self.scrolledwindow.add
,它会起作用。来自the documentation:
add_with_viewport()方法用于将没有本机滚动功能的窗口小部件(由子窗口指定)添加到滚动窗口。这是一个便利功能,相当于将子项添加到gtk.Viewport,然后将视口添加到滚动窗口。如果孩子有本机滚动(例如gtk.TextView,gtk.TreeView,gtk.Layout),请使用gtk.Container.add()而不是此方法。
gtk.TreeView
的“原生滚动”会将列标题保留在顶部,但如果您先将整个树视图窗口小部件放在gtk.Viewport
中,那么整个窗口小部件将会滚动。