Tk Treeview Focus()。如何获得多个选定的行?

时间:2018-02-19 13:58:27

标签: python tkinter treeview focus ttk

ttk.treeview.focus()返回单行的iid。树形视图框允许您选择多行。如何获取所选行的iid列表?

2 个答案:

答案 0 :(得分:3)

ttk.treeview.focus()返回当前焦点项。这意味着上次选择的项目。您正在寻找的功能是ttk.treeview.selection()。这将返回所选项目的元组。

答案 1 :(得分:1)

使用ttk.treeview.selection()

它提供所选项目。另请参见其他具有selection前缀的Treeview方法,例如

selection_add
selection_remove
selection_toggle

请参阅以下示例:

import tkinter as tk
from tkinter import ttk, Tk


def insert(tree, value):
    tree.insert('', tk.END, value, text=value)

root = Tk()
tree = ttk.Treeview(root)

insert(tree, '1')
insert(tree, '2')
insert(tree, '3')

tree.pack()
children = tree.get_children() 
tree.selection_set(children)
tree.selection_toggle(children[1])

# uncomment line by line to see the change
#tree.selection_toggle(children)
#tree.selection_remove(children[1])

print(tree.selection())

root.mainloop()