TreeView 中的 Gtk3+ Pygobject select 列

Gtk3+ Pygobject select columns in a TreeView

当用户单击 TreeView 中的单元格时,是否有一种有效的 way/a 方法来检索 ListModel 中 selected 单元格的列?或者当我调用 get_selected() 方法时,是否没有单元格之类的东西,只有行可以 selected 和 return 它们的 treeiter/model?

我想向用户显示一个数值矩阵,并允许他select一个,然后绘制这些值。

如果您手头有documentation/examples,请不要犹豫分享:)

编辑:我试图做的是将列 headers 上的点击事件连接到一个函数,该函数为我提供了我在创建期间在列文本中编码的列号。但这好像不太对..

如果有帮助的话,我可以创建一个最小的工作示例..

在 GtkTreeView 中访问 'columns' 比乍一看要复杂一些。原因之一是列实际上可以包含多个项目,然后显示为新列,即使它们是 'packed'.

识别列的一种方法是为每个列分配一个 sort_id,但这会使 headers 可点击,如果排序实际上不起作用,这是不自然的。

我设计了这个(有点狡猾的)方法:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  test_coord.py
#  
#  Copyright 2016 John Coppens <john@jcoppens.com>
#  
#  This program is free software```; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  


from gi.repository import Gtk

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())

        trview = Gtk.TreeView()
        tstore = Gtk.ListStore(str, str, str)
        renderer = Gtk.CellRendererText()
        for i in range(3):
            col = Gtk.TreeViewColumn("Col %d" % i, renderer, text = i)
            col.colnr = i
            trview.append_column(col)

        trview.set_model(tstore)
        trview.connect("button-press-event", self.on_pressed)

        for i in range(0, 15, 3):
            tstore.append((str(i), str(i+1), str(i+2)))

        self.add(trview)
        self.show_all()

    def on_pressed(self, trview, event):
        path, col, x, y = trview.get_path_at_pos(event.x, event.y)
        print("Column = %d, Row = %s" % (col.colnr, path.to_string()))

    def run(self):
        Gtk.main()


def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

这里的技巧是利用 Python 提供的可能性向现有 类 添加属性。所以我在每一列中添加了一个 colnr 并使用它来识别单击的单元格。在 C++ 中,有必要使用 set_dataget_data 方法来做同样的事情(在 Python 中不可用)。

使用 Gtk.TreeView.get_cursor() 可能是另一种方式,尽管我还没有在列包含打包数据的情况下对此进行测试(但并非每个 TreeView 都需要此功能)。

这使您可以直接访问 GtkTreePathGtkTreeViewColumn 匹配的当前焦点。

这是他的回答中@jcoppens 示例的略微修改版本:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  test_coord.py
#
#  Copyright 2016 John Coppens <john@jcoppens.com>
#
#  This program is free software```; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#


from gi.repository import Gtk


class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())

        self.trview = Gtk.TreeView()
        tstore = Gtk.ListStore(str, str, str)
        renderer = Gtk.CellRendererText()
        for i in range(3):
            col = Gtk.TreeViewColumn("Col %d" % i, renderer, text=i)
            # col.colnr = i
            self.trview.append_column(col)

        self.trview.set_model(tstore)
        self.trview.get_selection()\
            .connect("changed", self.on_selection_changed)

        for i in range(0, 15, 3):
            tstore.append((str(i), str(i+1), str(i+2)))

        self.add(self.trview)
        self.show_all()

    def on_selection_changed(self, selection):
        print("trview.get_cursor() returns: {}"
              .format(self.trview.get_cursor()))

    def run(self):
        Gtk.main()


def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0


if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))