简体   繁体   English

Tk Treeview Focus()。 如何获得多条选定线?

[英]Tk Treeview Focus(). How do I Get Multiple Selected Lines?

ttk.treeview.focus() returns the iid of a single line. ttk.treeview.focus()返回单行的iid。 The treeview box allows you to select multiple lines. 树状视图框允许您选择多行。 How do I get a list of iids for the selected lines? 如何获得所选行的iid列表?

ttk.treeview.focus() returns the current focus item. ttk.treeview.focus()返回当前焦点项目。 That means the item that was last selected. 这意味着最后选择的项目。 The function you are looking for is ttk.treeview.selection() . 您正在寻找的功能是ttk.treeview.selection() This returns a tuple of the selected items. 这将返回所选项目的元组。

Use ttk.treeview.selection() . 使用ttk.treeview.selection()

It gives the selected items. 它给出了选择的项目。 See also other Treeview methods with selection prefix such as, 另请参见其他具有selection前缀的Treeview方法,例如,

selection_add
selection_remove
selection_toggle

See the example below: 请参阅以下示例:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM