简体   繁体   English

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

[英]How to insert item selected from one treeview to another treeview

I have this code to select item from tkinter treeview to another tkinter treeview but when i select the item to insert it the id of item selected is inserted rather but not the content of the id.我有这段代码可以从tkinter treeview选择项目到另一个tkinter treeview但是当我选择要插入的项目时,会插入所选itemid而不是 id 的内容。

when i insert in the tree2 with this tree2.insert("", tk.END, values=n) it insert the last content of the List blow no matter what item i select.当我在插入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()

You could use current selection id, and pass its values as values instead:您可以使用当前选择 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'])

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

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