如何在 gtk.ScrolledWindow 内的 GtkTreeView 中显示列标题?

发布于 2024-11-30 16:12:45 字数 2181 浏览 4 评论 0原文

我在 Gtk.ScrolledWindow 中遇到 Gtk.Treeview 的问题。我有大量数据,这些数据不适合我的正常窗口几何形状,因此我将 Treeview 放入 ScrolledWindow 中,这似乎是正常方式。这个解决方案的问题是,当我滚动时,我的列标题消失了。这种行为对我来说似乎没什么问题,因为 ScrolledWindow 应该关心其中的小部件。

那么,如何使我的 Treeview 可滚动并且始终在第一行显示列标题?

我修改了 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()

I have a problem with Gtk.Treeview indide a Gtk.ScrolledWindow. I have a large amount of data, which doesnt fit inside my normal window geometrics, so i put my Treeview inside a ScrolledWindow, which seems to be the normal way. The problem with this solution is, that my Column Headers disapear, when I scroll. This behaviour seems somewhat ok to me, since the ScrolledWindow should care about the widgets inside of it.

So, how do I get my Treeview to be scrollable AND show the column headers always in the first row?

I modified the basictreeview.py example to represent my problem:

#!/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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹梦到西洲 2024-12-07 16:12:45

将 self.scrolledwindow.add_with_viewport 更改为 self.scrolledwindow.add 即可工作。来自文档

add_with_viewport() 方法用于将没有本机滚动功能的小部件(由 child 指定)添加到滚动窗口。这是一个方便的功能,相当于将子项添加到 gtk.Viewport,然后将视口添加到滚动窗口。如果子项具有本机滚动(例如 gtk.TextView、gtk.TreeView、gtk.Layout),请使用 gtk.Container.add() 而不是此方法。

gtk.TreeView 的“本机滚动”将列标题保留在顶部,但如果您首先将整个树视图小部件放入 gtk.Viewport 中,则整个小部件将会滚动。

Change self.scrolledwindow.add_with_viewport to self.scrolledwindow.add and it will work. From the documentation:

The add_with_viewport() method is used to add a widget (specified by child) without native scrolling capabilities to the scrolled window. This is a convenience function that is equivalent to adding child to a gtk.Viewport, then adding the viewport to the scrolled window. If a child has native scrolling (e.g. gtk.TextView, gtk.TreeView, gtk.Layout), use gtk.Container.add() instead of this method.

gtk.TreeView's "native scrolling" keeps the column headers at the top, but if you put the entire treeview widget in a gtk.Viewport first, then the entire widget will scroll.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文