【问题标题】:Gtk3+ Pygobject select columns in a TreeViewGtk3+ Pygobject 在 TreeView 中选择列
【发布时间】:2016-06-27 14:54:12
【问题描述】:

当用户单击 TreeView 中的单元格时,是否有一种有效的方法/方法来检索 ListModel 中选定单元格的列?或者当我调用 get_selected() 方法时,没有 Cells 之类的东西,只有 Rows 可以选择并返回它们的 treeiter/model?

我想向用户展示一个数值矩阵并允许他选择一个,然后将绘制哪些值。

如果您手头有任何文档/示例,请随时分享 :)

编辑:我试图做的是将列标题上的单击事件连接到一个函数,该函数为我提供了我在创建期间在列文本中编码的列号。但这似乎不太正确..

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

【问题讨论】:

    标签: python gtk3 pygobject


    【解决方案1】:

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

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

    我设计了这个(有点迂回的)方法:

    #!/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 中不可用)。

    【讨论】:

    • 这实际上是我想到的第二件事,当我尝试实施我的第一个方法时我已经这样做了:) 感谢您的帮助!
    【解决方案2】:

    使用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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      相关资源
      最近更新 更多