Search code examples
pythondictionarytkintertreeview

Displaying Dictionary Keys in Tkinter Treeview


I have a list of websites, the product title, price, and stock status as key pairs in a dictionary called "masterlist".

masterlist = [{'store': 'examplestore1.com', 'title': 'exampletitle1', 'price': '£99', 'stock': 'Sold Out'}, {'store': 'examplestore2.com', 'title': 'exampletitle2', 'price': '£12.99', 'stock': 'Sold Out'}]

I'm trying to display this in a simple treeview using tkinter, I have the treeview created but cannot insert the values. Any help is greatly appreciated!


Solution

  • You will need to run a loop that adds the values for each of the dictionaries in the list.

    The following code will work assuming the name of your treeview is 'treeview':

    for index, value in enumerate(masterlist):
        treeview.insert("", 'end', values=(
           masterlist[index]['store'],            
           masterlist[index]['title'],
           masterlist[index]['price'],
           masterlist[index]['stock'])
           )