树视图小部件中是否可以 select 多行?

Is it possible to select multiple rows in a treeview widget?

是否可以在树视图小部件中 select 多行?此外,您如何获得 input/selected 行。

这是我创建的树视图,但是我无法 select 两行。

如果这是不可能的,他们是 tkinter 或 ttk 中允许这样做的小部件吗?

是的,可以使用 selection() 方法获取 selected 项目的元组。获得元组后,您可以使用 item() 方法访问每个项目。这是一个例子。

P.S。在代码中,通过按 Ctrl 并单击行然后按 Enter

来 select 多个项目
import tkinter as tk
import tkinter.ttk

def select():
    <b>curItems = tree.selection()</b>
    tk.Label(root, text="\n".join([str(tree.item(i)['values']) for i in curItems])).pack()

root = tk.Tk()
tree = tkinter.ttk.Treeview(root, height=4)

tree['show'] = 'headings'
tree['columns'] = ('Badge Name', 'Requirement', 'Cost', 'Difficulty')
tree.heading("#1", text='Badge Name', anchor='w')
tree.column("#1", stretch="no")
tree.heading("#2", text='Requirement', anchor='w')
tree.column("#2", stretch="no")
tree.heading("#3", text='Cost', anchor='w')
tree.column("#3", stretch="no")
tree.heading("#4", text='Difficulty', anchor='w')
tree.column("#4", stretch="no")
tree.pack()

tree.insert("", "end", values=["IT Badge", "Track Computer", ".50", "2"])
tree.insert("", "end", values=["Selfless Badge", "Track Yourself", "0.50", "10"])
tree.insert("", "end", values=["Tracking Badge", "Track Animal", ".50", "7"])

tree.bind("<Return>", lambda e: select())

root.mainloop()