Home > front end >  how to convert treeview values into dictionary
how to convert treeview values into dictionary

Time:10-12

how to convert treeview values into a dictionary

if press the save button treeview values in the dictionary method

get column name and get column value [{'column_name':'column_value'}]

Example : [{'item':'apple','amount':'5000'},{'item':'orange','amount'='1000'}]

from tkinter import *
from tkinter import ttk

class sales:

    def __init__(self,root):
        self.root=root
        self.root.geometry("%dx%d 0 0" % (self.root.winfo_screenwidth(),self.root.winfo_screenheight()))
        self.root.state('zoomed')

        self.item=StringVar()
        self.amount=StringVar()

        lbl_name = Label(self.root, text="Enter Item \t:", font=("times new roman", 15, "bold"), bg="white").place(x=40, y=100)
        txt_name = Entry(self.root, textvariable=self.item, font=("times new roman", 15, "bold"), bg="white").place(x=320, y=100, width=350, height=35)

        lbl_name = Label(self.root, text="Amount \t:", font=("times new roman", 15, "bold"), bg="white").place(x=40, y=180)
        txt_name = Entry(self.root, textvariable=self.amount, font=("times new roman", 15, "bold"), bg="white").place(x=320, y=180, width=350, height=35)

        self.btn_add = Button(self.root, text="Add", command=self.add, font=("goudy old style", 15), bg="#4caf50",fg="white", cursor="hand2")
        self.btn_add.place(x=320, y=300, width=150, height=30)
        
        self.btn_select = Button(self.root, text="Save", command=self.save, font=("goudy old style", 15), bg="#4caf50",fg="white", cursor="hand2")
        self.btn_select.place(x=500, y=300, width=150, height=30)

# ==================== treeview ====================================

        tex_frame = Frame(self.root, bd=3, relief=RIDGE)
        tex_frame.place(x=320, y=400, width=700, height=300)

        scrolly = Scrollbar(tex_frame, orient=VERTICAL)
        scrollx = Scrollbar(tex_frame, orient=HORIZONTAL)

        self.tex_table = ttk.Treeview(tex_frame, columns=("item", "amount"), yscrollcommand=scrolly.set,
                                      xscrollcommand=scrollx.set)
        scrollx.pack(side=BOTTOM, fill=X)
        scrolly.pack(side=RIGHT, fill=Y)
        scrollx.config(command=self.tex_table.xview)
        scrolly.config(command=self.tex_table.yview)

        self.tex_table.heading("item", text="Item")
        self.tex_table.heading("amount", text="Amount")

        self.tex_table["show"] = "headings"
        self.tex_table.pack(fill=BOTH, expand=1)

    def add(self):

        self.tex_table.insert('',index="end",values=(self.item.get(),self.amount.get()))
        
    def save(self):
        pass
    
root=Tk()
obj=sales(root)
root.mainloop()

CodePudding user response:

Use treeview.get_children() and iterate through children id's returned by it, then make use of self.tex_table.item(x)["values"] to get the values of that row.

Here is an example

def save(self):
       
    values = []

    for x in self.tex_table.get_children():
        value_dict = {}
        
        for col, item in zip(self.tex_table["columns"], self.tex_table.item(x)["values"]):
            value_dict[col] = item
        
        values.append(value_dict)
    
    print(values)

  • Related