【问题标题】:Python - Gtk.TreeView with Gtk.ListStore get selected indexPython - Gtk.TreeView 与 Gtk.ListStore 获取选定索引
【发布时间】:2023-04-04 02:19:01
【问题描述】:

我想将项目存储在 gtk 列表中,我决定使用 Gtk TreeView 作为列表。因为我对 python 和 gtk 很陌生,所以我想将我的元素作为字符串表示形式存储在 TreeView 中,并在单独的 python 中列出相应的对象。

因此,如果我删除 Gtk 列表中的一个项目(通过键事件),我必须能够确定所选行索引以删除正确的 python-list 元素。

我的实际问题是:如何确定 Gtk.TreeView 中所选行的索引?

感谢您的帮助!

PS:我正在使用 Python 3 和 GTK 3

【问题讨论】:

    标签: python list gtk3 gtktreeview


    【解决方案1】:

    使用 Gtk.TreeViewSelection。您可以从 Gtk.TreeView 获取它(假设您的 GtkTreeModel(GtkListStore 或 GtkTreeStore)构造函数中有 id 列)。

        from gi.repository import Gtk
    
        def yourcallback ( selection ):
           model,list_iter = selection.get_selected ()
           if list_iter != None:
           # Debugging mode, uncomment to activate
           # print "row index = " + model[list_iter][column_number_that_you_want]
    
    
      # alternate
      # def yourcallback (path, column, data):
      #   ........
    
    
        ..........
        yourtvselection           = yourtv.get_selection ()        
        yourtvselection.connect ( "changed" , yourcallback)
        # alternative signal
        # yourtv.connect ("row-activated", yourcallback)
    

    【讨论】:

      猜你喜欢
      • 2015-05-31
      • 2015-09-24
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      相关资源
      最近更新 更多