Python - 带有 Gtk.ListStore 的 Gtk.TreeView 获取选定的索引

sch*_*rpp 3 python list gtktreeview gtk3

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

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

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

感谢您的帮助!

PS:我在 GTK 3 中使用 Python 3

小智 5

使用 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)
Run Code Online (Sandbox Code Playgroud)