将Treeview子级转移到Tkinter中的另一个Treeview

时间:2019-03-15 09:53:39

标签: tkinter treeview ttk

刚刚创建了一个帐户,我只想问一下如何将所有树视图数据行传输到另一个树视图中?我做了一个函数,可以在树视图中插入数据,我想在这些树视图中的另一个树视图中传输这些数据。另一个功能是,我设法转移了第一行,但是我很难做到这一点,任何帮助都深表感谢。对不起,英语不好。

这是我的主要树状视图:

    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 )

这是将数据添加到树视图中的函数:

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

这是创建另一个树视图的函数,这是我要显示主树视图中的数据的地方。

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

我的问题是我只能插入一行。但我想将所有行插入另一个树视图中。

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

1 个答案:

答案 0 :(得分:1)

您也应该同时用pythontkinter进行标记。

无论如何,这是将数据从一棵树传输到另一棵树的方法。

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