how to create table in tkinter

Solutions on MaxInterview for how to create table in tkinter by the best coders in the world

showing results for - "how to create table in tkinter"
Clara
22 Sep 2018
1# how to create a table in tkinter using an example
2# try and run it for better understanding
3from tkinter import *
4# creating a class for creating table in tkinter window
5class Table:
6    def __init__(self,root_2):
7        global results
8        for i in range(l):
9            for j in range(5): # here we use dynamic variable so that you can edit and access the table elements if needed
10                exec(f"self.e{i}_{j} = Entry(root_2,width=20,fg='blue',font=('Arial',16,'bold'))")
11                exec(f"self.e{i}_{j}.grid(row={i}+2,column={j})")
12                exec("self.e"+str(i)+"_"+str(j)+".insert(END, results["+str(i)+"]["+str(j)+"])")
13# the data to be placed in table
14results = [['1001','SID','CS','100000','18'],['1002','KRISH','BioTech','100000','18'],['1003','GEM','CS','100000','18']]                   
15l = len(results)
16# main window where the table will be
17root_2 = Tk()
18root_2.title('Details')
19text = Label(root_2,text = "EMPLOYEE DETAILS", font=('Atial',18,'bold')).grid(row=0,column=2)
20text = Label(root_2,text = "ID", font=('Atial',16,'bold')).grid(row=1,column=0,sticky=W)
21text = Label(root_2,text = "Name", font=('Atial',16,'bold')).grid(row=1,column=1,sticky=W)
22text = Label(root_2,text = "Department", font=('Atial',16,'bold')).grid(row=1,column=2,sticky=W)
23text = Label(root_2,text = "Salary", font=('Atial',16,'bold')).grid(row=1,column=3,sticky=W)
24text = Label(root_2,text = "Age", font=('Atial',16,'bold')).grid(row=1,column=4,sticky=W)
25# this is where the class to create the table is called
26t = Table(root_2)
27root_2.mainloop()