捕获 PyGTK TreeView 重新排序

发布于 2024-09-01 03:31:22 字数 292 浏览 12 评论 0原文

我有一个简单的 gtk.TreeView ,带有 gtk.ListStore 模型和 set_reorderable(True) ,我想捕获时发出的信号/事件用户通过拖放列表重新排序,但文档没有多大帮助:

“应用程序可以通过连接到模型的信号来监听这些更改”

所以我尝试连接模型(ListStore)信号...但令人惊讶! ListStore 没有信号,因此您被分派到 TreeModel 信号,然后我尝试与 TreeModel“行重新排序”信号连接,但没有幸运。

我应该如何捕获用户执行的列表重新排序?

I have a simple gtk.TreeView with a gtk.ListStore model and set_reorderable(True), I want to catch the signal/event emited when the user reorder through drag&drop the list, but the documentation does not help much:

"The application can listen to these changes by connecting to the model's signals"

So I tried to connect the model (ListStore) signals... but surprise! ListStore has no signals, so you are dispatched to TreeModel signals, then I tried to connect with the TreeModel "rows-reordered" signal with no lucky.

How should I catch the list reorder performed by the user?

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

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

发布评论

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

评论(2

_畞蕅 2024-09-08 03:31:22

目前 PyGTK 中还没有办法做到这一点。 “rows-reordered”是正确的信号,但除了“以某种方式重新排序”之外,在 PyGTK 中不可能从中得出任何信息。在 C GTK+ 中,您可以使用相同的信号并在回调中获取所需的信息,但在 Python 中则不然。

There is no way to do that in PyGTK currently. "rows-reordered" is the correct signal, but it is impossible to derive any information from it in PyGTK other than "somehow reordered". In C GTK+ you could use the same signal and get the required information in callback, but not in Python.

守护在此方 2024-09-08 03:31:22

我也遇到这个问题,文档不清楚。但这是我发现的,

时,会发出“行重新排序”信号

tvcolumn.set_sort_column_id(0)

当您有但是您仍然将信号绑定到树模型

treestore = gtk.TreeStore(str, object)
treestore.connect("rows-reordered", self.rows_r)

。这将导致可见的列标题变得可单击。当您单击列标题时,它将按升序对树中的项目重新排序,如果再次单击它,则会按降序排列,如此反复。

这是一个简单的代码,您可以测试一下并了解我的意思。

import pygtk
pygtk.require('2.0')
import gtk

class BasicTreeViewExample:

    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Treeview")
        window.set_size_request(200, 200)
        window.connect("destroy", lambda w: gtk.main_quit())

        treestore = gtk.TreeStore(str)
        treestore.connect("rows-reordered", self.rows_reordered)

        for i in range(4):
            piter = treestore.append(None, ['Item %i' % i])

        treeview = gtk.TreeView(treestore)

        tvcolumn = gtk.TreeViewColumn('Click Me!')

        treeview.append_column(tvcolumn)

        cell = gtk.CellRendererText()

        tvcolumn.pack_start(cell, True)

        tvcolumn.add_attribute(cell, 'text', 0)

        # This allows the column header ("Click me!") to be clickable and sort/order items
        tvcolumn.set_sort_column_id(0)

        window.add(treeview)
        window.show_all()


    def rows_reordered(self, a, b, c, d):
        print a
        print b
        print c
        print d


def main():
    gtk.main()

if __name__ == "__main__":
    tvexample = BasicTreeViewExample()
    main()

I too had this problem and the docs are unclear. But here's what I found

'rows-reordered' signal is emitted when you have

tvcolumn.set_sort_column_id(0)

However you still bind the signal to the treemodel.

treestore = gtk.TreeStore(str, object)
treestore.connect("rows-reordered", self.rows_r)

That will cause the visible column header to become clickable. When you click on the column header, it will reorder the items in the tree in ascending order and then in descending order if you click it again, and back and forth.

Here's a simple code you can test and see what i mean.

import pygtk
pygtk.require('2.0')
import gtk

class BasicTreeViewExample:

    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Treeview")
        window.set_size_request(200, 200)
        window.connect("destroy", lambda w: gtk.main_quit())

        treestore = gtk.TreeStore(str)
        treestore.connect("rows-reordered", self.rows_reordered)

        for i in range(4):
            piter = treestore.append(None, ['Item %i' % i])

        treeview = gtk.TreeView(treestore)

        tvcolumn = gtk.TreeViewColumn('Click Me!')

        treeview.append_column(tvcolumn)

        cell = gtk.CellRendererText()

        tvcolumn.pack_start(cell, True)

        tvcolumn.add_attribute(cell, 'text', 0)

        # This allows the column header ("Click me!") to be clickable and sort/order items
        tvcolumn.set_sort_column_id(0)

        window.add(treeview)
        window.show_all()


    def rows_reordered(self, a, b, c, d):
        print a
        print b
        print c
        print d


def main():
    gtk.main()

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