如何将从一个树视图中选择的项目插入到另一个树视图中

时间:2018-02-26 11:31:08

标签: python tkinter

我有此代码可以选择tkinter treeview到另一个tkinter treeview的项目,但是当我选择要插入的项目时,会选择id所选的item但不会id的内容。

当我使用此tree2插入tree2.insert("", tk.END, values=n)时,无论我选择哪个项目,它都会插入List打击的最后一个内容。

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

1 个答案:

答案 0 :(得分:1)

您可以使用当前选择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'])