Python Tkinter Populate Treeview From List

How To Populate a Tkinter TreeView From a List Using Python

Python Tkinter Populate Treeview From List


import tkinter as tk
from tkinter import *
from tkinter import ttk


root = Tk()


data = [
[1,"AAA","BBB","ab@mail.com",17],
[3,"EEE","FFF","ef@mail.com",91],
[4,"GGG","HHH","gh@mail.com",47],
[7,"MMM","NNN","mn@mail.com",25],
[8,"PPP","QQQ","pq@mail.com",43],
[9,"RRR","SSS","rs@mail.com",94],
]


trv = ttk.Treeview(root, columns=(1,2,3,4,5), show='headings')
trv.column(1, anchor='center', width=100)
trv.column(2, anchor='center', width=100)
trv.column(3, anchor='center', width=100)
trv.column(4, anchor='center', width=100)
trv.column(5, anchor='center', width=100)

trv.heading(1, text='ID')
trv.heading(2, text='First Name')
trv.heading(3, text='Last Name')
trv.heading(4, text='Email')
trv.heading(5, text='Age')

# create a function to display data in treeview
def displayData():
for row in data:
trv.insert('',END, values=row)


displayData()


trv.grid(row=0, column=0, padx=10, pady=10)


root.mainloop()

OUTPUT:

Python Tkinter Populate Treeview
Python Tkinter Populating Treeview





Share this

Related Posts

Previous
Next Post »