transfer treeview children to another treeview in tkinter

I just created an account on Stack Overflow so I could ask how can I transfer all the treeview rows of data to another treeview? I made a function to insert data in the treeview and I want to transfer these data in another treeview in another function. I managed to transfer the first row only. I'm having a hard time accomplishing this, any kind of help is deeply appreciated. Sorry for poor grammar.

Here is my main treeview:

cartTreeView = ttk.Treeview( posWindowCanvas )
cartTreeView[ "show" ] = "headings"
cartTreeView[ "columns" ] = ( "Sales ID", "Product Code", "Name", "Price", "Quantity", "Total" )
cartTreeView.heading( "Sales ID", text = "Sales ID", anchor = W )
cartTreeView.heading( "Product Code", text = "Product Code", anchor = W )
cartTreeView.heading( "Name", text = "Name", anchor = W )
cartTreeView.heading( "Price", text = "Price", anchor = W )
cartTreeView.heading( "Quantity", text = "Quantity", anchor = W )
cartTreeView.heading( "Total", text = "Total", anchor = W )

and here is the function that adds data into the treeview:

def addNow():

    getTreeValue = productsTreeView.focus()
    selected = productsTreeView.item( getTreeValue )
    total = float( selected["values"][2] ) * productQuantity.get()
    roundedUp = round( total, 2 )
    cartTreeView.insert( "", "end" , text = "", values = ( "000", str(selected["values"][0]), selected["values"][1], selected["values"][2], productQuantity.get(), roundedUp ) )

and here is the function that creates another treeview, and this is where i want the data from the main treeview to be displayed.

def transactNow():


    transactNowWindow = Toplevel()
    transactNowWindow.title( "Transaction Window" )
    transactNowWindow.geometry( "725x290" )
    transactNowWindow.resizable( height = False, width = False )

    transactNowWindowCanvas = Canvas( transactNowWindow, height = 550, width = 280, bg = "gray84" )
    transactNowWindowCanvas.pack( fill = BOTH )


    
    cartTreeView2 = ttk.Treeview( transactNowWindowCanvas )
    cartTreeView2[ "show" ] = "headings"
    cartTreeView2[ "columns" ] = ( "Sales ID", "Product Code", "Name", "Price", "Quantity", "Total" )
    cartTreeView2.heading( "Sales ID", text = "Sales ID", anchor = W )
    cartTreeView2.heading( "Product Code", text = "Product Code", anchor = W )
    cartTreeView2.heading( "Name", text = "Name", anchor = W )
    cartTreeView2.heading( "Price", text = "Price", anchor = W )
    cartTreeView2.heading( "Quantity", text = "Quantity", anchor = W )
    cartTreeView2.heading( "Total", text = "Total", anchor = W )
    cartTreeView2.column( "Sales ID", minwidth = 0, width = 55, stretch = NO )
    cartTreeView2.column( "Product Code", minwidth = 0, width = 90, stretch = NO )
    cartTreeView2.column( "Name", minwidth = 0, width = 325, stretch = NO )
    cartTreeView2.column( "Price", minwidth = 0, width = 80, stretch = NO )
    cartTreeView2.column( "Quantity", minwidth = 0, width = 55, stretch = NO )
    cartTreeView2.column( "Total", minwidth = 0, width = 80, stretch = NO )
    cartTreeView2.place( x = 10, y = 10 )

    cartTreeViewScrollBar = ttk.Scrollbar( transactNowWindowCanvas, orient = "vertical", command = cartTreeView2.yview )
    cartTreeViewScrollBar.place( x = 698, y = 10, height = 227 )
    cartTreeView2.configure( yscrollcommand = cartTreeViewScrollBar.set )

    for child in cartTreeView.get_children():
            cartList.append( cartTreeView.item( child )["values"] )

    
    cartTreeView2.insert( "", "end", text = "", values = ( cartList[0][0], cartList[0][1], cartList[0][2], cartList[0][3], cartList[0][4], cartList[0][5] ) )
    
        

My problem is I can only just insert one row, but I want to insert all the rows in another treeview.

        for child in cartTreeView.get_children():
                cartList.append( cartTreeView.item( child )["values"] )

        
        cartTreeView2.insert( "", "end", text = "", values = ( cartList[0][0], cartList[0][1], cartList[0][2], cartList[0][3], cartList[0][4], cartList[0][5] ) )
        
        

Upvotes: 0

Views: 1393

Answers (1)

Henry Yik
Henry Yik

Reputation: 22503

You should have tagged this with python and tkinter as well.

Anyway, here's how to transfer data from 1 tree to another.

from tkinter import *
from tkinter import ttk

root = Tk()

tree_a = ttk.Treeview(root)
tree_a.pack()
tree_a["columns"] = ("1","2","3","4","5")
for i in range(5):
    tree_a.insert("",0,text=i,values=(i,i,i,i,i))

tree_b = ttk.Treeview(root)
tree_b.pack()
tree_b["columns"] = ("1","2","3","4","5")

def click():
    for child in tree_a.get_children():
        tree_b.insert("",0,text=tree_a.item(child)["text"],values=tree_a.item(child)["values"])

Button(root,text="Click",command=click).pack()

root.mainloop()

Upvotes: 1

Related Questions