AD WAN
AD WAN

Reputation: 1474

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.

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.

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

Upvotes: 1

Views: 1490

Answers (1)

Nae
Nae

Reputation: 15325

You could use current selection id, and pass its values as values instead:

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'])

Upvotes: 1

Related Questions