我有这段代码可以从tkinter treeview
中选择项目到另一个tkinter treeview
,但是当我选择要插入的项目时,它的id
item
selected 被插入,而不是 id 的内容。
当我使用 tree2.insert("", tk.END,values=n)
插入 tree2
时,它会插入 的最后内容无论我选择什么项目,都会列出
。
from tkinter import ttk
import tkinter as tk
blow = [("january", "2013"),("february", "2014"),("march", "2015"),("april",
"2016"),("may", "2017")]
def append_select():
for my in tree.selection():
tree2.insert("", tk.END, values=my)
# tree2.insert("", tk.END, values=n) # this insert last content in the list
root = tk.Tk()
root.geometry("500x500")
tree = ttk.Treeview(columns=("columns1", "columns"), show="headings",
selectmode="browse")
tree.heading("#1", text="Month")
tree.heading("#2", text="Year")
for n in blow:
tree.insert("", tk.END, values=(n))
tree.pack()
b1 = tk.Button(text="append", command=append_select)
b1.pack()
tree2 = ttk.Treeview(columns=("Month", "Year"), show="headings")
tree2.heading("#1", text="First name")
tree2.heading("#2", text="Surname")
tree2.pack()
root.mainloop()
您可以使用当前选择 ID,并将其值作为值传递:
def append_select():
cur_id = tree.focus()
if cur_id: # do nothing if there's no selection
tree2.insert("", tk.END, values=tree.item(cur_id)['values'])
我是一名优秀的程序员,十分优秀!