Python Tkinter Table Tutorial

In this Python Tkinter tutorial, we will learn How to create a Table in Python Tkinter and we will also cover different examples related to Tkinter Table. And, we will cover these topics.

  • Python Tkinter Table
  • Python Tkinter Table with Scrollbar
  • Python Tkinter Table Widget
  • Python Tkinter Table Input
  • Python Tkinter Table List
  • Python Tkinter Table Refresh
  • Python Tkinter Table Canvas
  • Python Tkinter Table Sort
  • Python Tkinter Editable Table

Python Tkinter Table

In this section, we learn about the Python Tkinter table and we create a Tkinter table with the help of Treeview. Treeview refers to hierarchical representation.

The Tkinter.ttk module is used to drive a tree view and we use the tree view to make a table. A table is useful to display data or information that is visible in form of rows and columns.

Code:

  • In this code, we take mygame .column() to represent the columns in Python Tkinter Table.
  • mygame.heading() is used to display the headings in the Python Tkinter Table.
  • mygame.insert() is used to insert the values in the Python Tkinter Table.
  • ttk.Treeview() helps us to create a table.
  • ‘bg‘ is defining the background color with ‘#AC99F2’.
from tkinter import *
from  tkinter import ttk


ws  = Tk()
ws.title('PythonGuides')
ws.geometry('500x500')
ws['bg'] = '#AC99F2'

game_frame = Frame(ws)
game_frame.pack()

my_game = ttk.Treeview(game_frame)

my_game['columns'] = ('player_id', 'player_name', 'player_Rank', 'player_states', 'player_city')

my_game.column("#0", width=0,  stretch=NO)
my_game.column("player_id",anchor=CENTER, width=80)
my_game.column("player_name",anchor=CENTER,width=80)
my_game.column("player_Rank",anchor=CENTER,width=80)
my_game.column("player_states",anchor=CENTER,width=80)
my_game.column("player_city",anchor=CENTER,width=80)

my_game.heading("#0",text="",anchor=CENTER)
my_game.heading("player_id",text="Id",anchor=CENTER)
my_game.heading("player_name",text="Name",anchor=CENTER)
my_game.heading("player_Rank",text="Rank",anchor=CENTER)
my_game.heading("player_states",text="States",anchor=CENTER)
my_game.heading("player_city",text="States",anchor=CENTER)

my_game.insert(parent='',index='end',iid=0,text='',
values=('1','Ninja','101','Oklahoma', 'Moore'))
my_game.insert(parent='',index='end',iid=1,text='',
values=('2','Ranger','102','Wisconsin', 'Green Bay'))
my_game.insert(parent='',index='end',iid=2,text='',
values=('3','Deamon','103', 'California', 'Placentia'))
my_game.insert(parent='',index='end',iid=3,text='',
values=('4','Dragon','104','New York' , 'White Plains'))
my_game.insert(parent='',index='end',iid=4,text='',
values=('5','CrissCross','105','California', 'San Diego'))
my_game.insert(parent='',index='end',iid=5,text='',
values=('6','ZaqueriBlack','106','Wisconsin' , 'TONY'))

my_game.pack()

ws.mainloop()

Output:

After running this example, we have the following output which shows the data in the form of rows and columns inside a table.

Python Tkinter Table
Python Tkinter Table

Read: Python Tkinter Quiz – Complete tutorial

Python Tkinter Table with Scrollbar

In this section, we will cover How to create a python Tkinter table with scrollbars.

  • At times, the screen size is not enough to display the entire data. So to navigate around the data scrollbars are used.
  • There are two types of scrollbars:
    • Horizontal Scrollbar
    • Vertical Scrollbar
  1. Horizontal Scrollbar allows you to navigate in right and left directions.
  2. Vertical Scrollbar allows you to navigate in Up and down directions.

There are two ways to implement a scrollbar. The first is to implement directly on the main window and the second way is to create a frame and put other widgets on the LEFT and scrollbar on the RIGHT.

Source code :

In this example, we have demonstrated horizontal and vertical scrollbars in Python Tkinter.

from tkinter import *
from  tkinter import ttk


ws  = Tk()
ws.title('PythonGuides')
ws.geometry('500x500')
ws['bg'] = '#AC99F2'

game_frame = Frame(ws)
game_frame.pack()

#scrollbar
game_scroll = Scrollbar(game_frame)
game_scroll.pack(side=RIGHT, fill=Y)

game_scroll = Scrollbar(game_frame,orient='horizontal')
game_scroll.pack(side= BOTTOM,fill=X)

my_game = ttk.Treeview(game_frame,yscrollcommand=game_scroll.set, xscrollcommand =game_scroll.set)


my_game.pack()

game_scroll.config(command=my_game.yview)
game_scroll.config(command=my_game.xview)

#define our column
 
my_game['columns'] = ('player_id', 'player_name', 'player_Rank', 'player_states', 'player_city')

# format our column
my_game.column("#0", width=0,  stretch=NO)
my_game.column("player_id",anchor=CENTER, width=80)
my_game.column("player_name",anchor=CENTER,width=80)
my_game.column("player_Rank",anchor=CENTER,width=80)
my_game.column("player_states",anchor=CENTER,width=80)
my_game.column("player_city",anchor=CENTER,width=80)

#Create Headings 
my_game.heading("#0",text="",anchor=CENTER)
my_game.heading("player_id",text="Id",anchor=CENTER)
my_game.heading("player_name",text="Name",anchor=CENTER)
my_game.heading("player_Rank",text="Rank",anchor=CENTER)
my_game.heading("player_states",text="States",anchor=CENTER)
my_game.heading("player_city",text="States",anchor=CENTER)

#add data 
my_game.insert(parent='',index='end',iid=0,text='',
values=('1','Ninja','101','Oklahoma', 'Moore'))
my_game.insert(parent='',index='end',iid=1,text='',
values=('2','Ranger','102','Wisconsin', 'Green Bay'))
my_game.insert(parent='',index='end',iid=2,text='',
values=('3','Deamon','103', 'California', 'Placentia'))
my_game.insert(parent='',index='end',iid=3,text='',
values=('4','Dragon','104','New York' , 'White Plains'))
my_game.insert(parent='',index='end',iid=4,text='',
values=('5','CrissCross','105','California', 'San Diego'))
my_game.insert(parent='',index='end',iid=5,text='',
values=('6','ZaqueriBlack','106','Wisconsin' , 'TONY'))
my_game.insert(parent='',index='end',iid=6,text='',
values=('7','RayRizzo','107','Colorado' , 'Denver'))
my_game.insert(parent='',index='end',iid=7,text='',
values=('8','Byun','108','Pennsylvania' , 'ORVISTON'))
my_game.insert(parent='',index='end',iid=8,text='',
values=('9','Trink','109','Ohio' , 'Cleveland'))
my_game.insert(parent='',index='end',iid=9,text='',
values=('10','Twitch','110','Georgia' , 'Duluth'))
my_game.insert(parent='',index='end',iid=10,text='',
values=('11','Animus','111', 'Connecticut' , 'Hartford'))
my_game.pack()


ws.mainloop()

In the above example, we are using game_scroll.pack(side=RIGHT,fill=Y) for horizontal scrollbar and game_scroll.pack(side= BOTTOM,fill=X) for the vertical scrollbar.

Output:

Python Tkinter Table with Scrollbar
Python Tkinter Table with Scrollbar

Read: Python QR code generator using pyqrcode in Tkinter

Python Tkinter Table Widget

The table is a tabular representation of data in rows and column format.
Python Tkinter does not have a specific Table widget using which we can create a table.

Python Tkinter Table widget or Python Tk Table widget can be created using List, Pandas, or Treeview from the ttk package.

READ:  How to read a CSV to the dictionary using Pandas in Python [3 Examples]

Let us understand with the help of an example.

from tkinter import *
from  tkinter import ttk


ws  = Tk()
ws.title('PythonGuides')
ws.geometry('300x400')

set = ttk.Treeview(ws)
set.pack()

set['columns']= ('id', 'full_Name','award')
set.column("#0", width=0,  stretch=NO)
set.column("id",anchor=CENTER, width=80)
set.column("full_Name",anchor=CENTER, width=80)
set.column("award",anchor=CENTER, width=80)

set.heading("#0",text="",anchor=CENTER)
set.heading("id",text="ID",anchor=CENTER)
set.heading("full_Name",text="Full_Name",anchor=CENTER)
set.heading("award",text="Award",anchor=CENTER)

set.insert(parent='',index='end',iid=0,text='',
values=('101','john','Gold'))
set.insert(parent='',index='end',iid=1,text='',
values=('102','jack',"Silver"))
set.insert(parent='',index='end',iid=2,text='',
values=('103','joy','Bronze'))

ws.mainloop()

Here is the output of the above example.

Python Table Widget
Python Tkinter Table with Scrollbar

Read: How to Create a Snake Game in Python Tkinter

Python Tkinter Table Input

In this section, we will learn how to create a table with input fields in Python Tkinter.

  • Firstly, we will create a entry field where we will give the input, and the input which we give will be shown in the table.
  • And it will also have the following fileds
    • Labels: labels which describe the word or term.
    • Entry: Entry where we can give the input.

Code :

So, in this example, we are using the entry box for taking the input. And when we enter the data as input in the entry box, the data will show in the table.

from tkinter import *
from tkinter import ttk

ws=Tk()

ws.title('PythonGuides')
ws.geometry('500x500')

set = ttk.Treeview(ws)
set.pack()

set['columns']= ('id', 'full_Name','award')
set.column("#0", width=0,  stretch=NO)
set.column("id",anchor=CENTER, width=80)
set.column("full_Name",anchor=CENTER, width=80)
set.column("award",anchor=CENTER, width=80)

set.heading("#0",text="",anchor=CENTER)
set.heading("id",text="ID",anchor=CENTER)
set.heading("full_Name",text="Full_Name",anchor=CENTER)
set.heading("award",text="Award",anchor=CENTER)

#data
data  = [
    [1,"Jack","gold"],
    [2,"Tom","Bronze"]

]

global count
count=0
    
for record in data:
      
    set.insert(parent='',index='end',iid = count,text='',values=(record[0],record[1],record[2]))
       
    count += 1



Input_frame = Frame(ws)
Input_frame.pack()

id = Label(Input_frame,text="ID")
id.grid(row=0,column=0)

full_Name= Label(Input_frame,text="Full_Name")
full_Name.grid(row=0,column=1)

award = Label(Input_frame,text="Award")
award.grid(row=0,column=2)

id_entry = Entry(Input_frame)
id_entry.grid(row=1,column=0)

fullname_entry = Entry(Input_frame)
fullname_entry.grid(row=1,column=1)

award_entry = Entry(Input_frame)
award_entry.grid(row=1,column=2)

def input_record():
    

    global count
   
    set.insert(parent='',index='end',iid = count,text='',values=(id_entry.get(),fullname_entry.get(),award_entry.get()))
    count += 1

   
    id_entry.delete(0,END)
    fullname_entry.delete(0,END)
    award_entry.delete(0,END)
     
#button
Input_button = Button(ws,text = "Input Record",command= input_record)

Input_button.pack()



ws.mainloop()

Output:

Python Tkinter Table Input Example
Python Tkinter Table Input

In this table, we already have some data and now to add more data to the list, we can give input in the entry box. And the input which we give in the entry by clicking on the input record button the data will show on the table.

Python Tkinter Table input
Python Tkinter Table Input

After click on the Input Record, the input which we gave in the entry box will show on the table.

Python Tkinter Table Input
Python Tkinter Table Input example

Read: Python Tkinter Image

Python Tkinter Table List

In this section, we will learn how to create a table using a list in Python Tkinter.

  • We create the list by using treeview, in which we insert data to make a list insert(parent=”,index, iid= None ,text=”).
  • Parent: The Parent is the item, or empty string to create top level item.
  • The index is an integer or value end.
  • Here, iid is an identifier, iid is not present in the table , a new different identifier is generated all time.

Code :

from tkinter import *
from  tkinter import ttk
ws = Tk()

ws.title('PythonGuides')
ws.geometry('500x500')

list = ttk.Treeview(ws)
list.pack()

list['columns']= ('Name','Gender','Title')

list.column("#0", width=0,  stretch=NO)
list.column("Name",anchor=CENTER, width=80)
list.column("Gender",anchor=CENTER, width=80)
list.column("Title",anchor=CENTER, width=80)

list.heading("#0",text="",anchor=CENTER)
list.heading("Name",text="P_Name",anchor=CENTER)
list.heading("Gender",text="P_Gender",anchor=CENTER)
list.heading("Title",text="P_Title",anchor=CENTER)


list.insert(parent='',index='end',iid=0,text='',
values=('Todd S Core','Male','Mr'))
list.insert(parent='',index='end',iid=1,text='',
values=('Thomas C Wood','Male','Mr'))
list.insert(parent='',index='end',iid=2,text='',
values=('Misha J McKinney','Female','Mrs'))
list.insert(parent='',index='end',iid=3,text='',
values=('Teresa B Haight','Female','Ms'))
list.insert(parent='',index='end',iid=4,text='',
values=('Michael L McLaurin','Male','Mr'))
list.insert(parent='',index='end',iid=5,text='',
values=('David S Ward','Male','Mr'))
list.insert(parent='',index='end',iid=6,text='',
values=('Carolyn G Price','Feale','Mrs'))
list.insert(parent='',index='end',iid=7,text='',
values=('Diana D Lai','Female','Ms'))
list.insert(parent='',index='end',iid=8,text='',
values=('Bonnie E Duran','Female','Ms'))
list.insert(parent='',index='end',iid=9,text='',
values=('Joseph M Munger','Male','Mr'))


ws.mainloop()

In this output, the data is display in the form of a Table list. We can store the data in form of numbers or strings. This table list only shows the string we can also put numbers in the table list.

Python Tkinter Table list Example
Python Tkinter Table List

Read: Python Tkinter Colors

Python Tkinter Table Refresh

In this section, we will learn how to refresh table data in Python Tkinter.

The Refresh function is meant to be used when we update anything inside our table data. And that updated data will be visible in the table once the table is refreshed.

  • In the below code, we have used labels like name, ID, and rank. And we have also added two buttons.
  • One button is selecting the records that was filled by user and another button will be used in updating the record and refresh the updated results inside the table.

Code:

Let’s understand this code with an example and the code for the example is as follows.

from tkinter import *
from  tkinter import ttk


ws  = Tk()
ws.title('PythonGuides')
ws.geometry('500x500')
ws['bg'] = '#AC99F2'

game_frame = Frame(ws)
game_frame.pack()

#scrollbar
game_scroll = Scrollbar(game_frame)
game_scroll.pack(side=RIGHT, fill=Y)

game_scroll = Scrollbar(game_frame,orient='horizontal')
game_scroll.pack(side= BOTTOM,fill=X)

my_game = ttk.Treeview(game_frame,yscrollcommand=game_scroll.set, xscrollcommand =game_scroll.set)


my_game.pack()

game_scroll.config(command=my_game.yview)
game_scroll.config(command=my_game.xview)

#define our column
 
my_game['columns'] = ('player_id', 'player_name', 'player_Rank')

# format our column
my_game.column("#0", width=0,  stretch=NO)
my_game.column("player_id",anchor=CENTER, width=80)
my_game.column("player_name",anchor=CENTER,width=80)
my_game.column("player_Rank",anchor=CENTER,width=80)


#Create Headings 
my_game.heading("#0",text="",anchor=CENTER)
my_game.heading("player_id",text="Id",anchor=CENTER)
my_game.heading("player_name",text="Name",anchor=CENTER)
my_game.heading("player_Rank",text="Rank",anchor=CENTER)


#add data 
my_game.insert(parent='',index='end',iid=0,text='',
values=('1','Ninja','101'))
my_game.insert(parent='',index='end',iid=1,text='',
values=('2','Ranger','102'))
my_game.insert(parent='',index='end',iid=2,text='',
values=('3','Deamon','103'))
my_game.insert(parent='',index='end',iid=3,text='',
values=('4','Dragon','104'))
my_game.insert(parent='',index='end',iid=4,text='',
values=('5','CrissCross','105'))
my_game.insert(parent='',index='end',iid=5,text='',
values=('6','ZaqueriBlack','106'))
my_game.insert(parent='',index='end',iid=6,text='',
values=('7','RayRizzo','107'))
my_game.insert(parent='',index='end',iid=7,text='',
values=('8','Byun','108'))
my_game.insert(parent='',index='end',iid=8,text='',
values=('9','Trink','109'))
my_game.insert(parent='',index='end',iid=9,text='',
values=('10','Twitch','110'))
my_game.insert(parent='',index='end',iid=10,text='',
values=('11','Animus','111'))
my_game.pack()

frame = Frame(ws)
frame.pack(pady=20)

#labels
playerid= Label(frame,text = "player_id")
playerid.grid(row=0,column=0 )

playername = Label(frame,text="player_name")
playername.grid(row=0,column=1)

playerrank = Label(frame,text="Player_rank")
playerrank.grid(row=0,column=2)

#Entry boxes
playerid_entry= Entry(frame)
playerid_entry.grid(row= 1, column=0)

playername_entry = Entry(frame)
playername_entry.grid(row=1,column=1)

playerrank_entry = Entry(frame)
playerrank_entry.grid(row=1,column=2)

#Select Record
def select_record():
    #clear entry boxes
    playerid_entry.delete(0,END)
    playername_entry.delete(0,END)
    playerrank_entry.delete(0,END)
    
    #grab record
    selected=my_game.focus()
    #grab record values
    values = my_game.item(selected,'values')
    #temp_label.config(text=selected)

    #output to entry boxes
    playerid_entry.insert(0,values[0])
    playername_entry.insert(0,values[1])
    playerrank_entry.insert(0,values[2])

#save Record
def update_record():
    selected=my_game.focus()
    #save new data 
    my_game.item(selected,text="",values=(playerid_entry.get(),playername_entry.get(),playerrank_entry.get()))
    
   #clear entry boxes
    playerid_entry.delete(0,END)
    playername_entry.delete(0,END)
    playerrank_entry.delete(0,END)

#Buttons
select_button = Button(ws,text="Select Record", command=select_record)
select_button.pack(pady =10)

refresh_button = Button(ws,text="Refresh Record",command=update_record)
refresh_button.pack(pady = 10)

temp_label =Label(ws,text="")
temp_label.pack()

ws.mainloop()

In the above code, my_game.focus() is used to grab the record, and my_game.item() is used to grab the record value.

READ:  Matplotlib plot bar chart

Output:

After running the above code, we will have this output in which we have some entries of record which having labels as player_id, Player_name, Player_record. And we can also see the buttons to select and refresh the records.

Python Tkinter Table refresh
Python Tkinter Table Refresh

In this, we are selecting the record and updating the Player_name and Player_rank.

Python Tkinter Table refresh and select
Python Tkinter Table Refresh example

In this, we have updated the record and after clicking on the Refresh button the records will be updated.

Python Tkinter Table refresh records
Refreshing Results

Read: Python Tkinter Autocomplete

Python Tkinter Table Canvas

In this section, we will learn to create a Table using canvas in Python Tkinter. The canvas is used for drawing pictures, graphics text, or frames.

In this example, we have demonstrated a table using canvas. And for this, we have created a table of 3 rows and each row contains 3 squares.

Code:

from tkinter import *

list = [[0,0,0,0], [0, 0, 0, 0], [0, 0, 0, 0]]
a = len(list)     
length = 300//a 
ws = Tk()
ws.geometry("500x500")


canvas = Canvas(ws, width=500, height=500, bg="#7698A6")
canvas.pack(side=RIGHT)

for i in range(a):
    y = i * length
    for j in range(a):
        x = j * length
        canvas.create_rectangle(x, y, x+length, y+length, fill="#D97E4A")

f = Frame(ws, width=200, height=500, bg="#F23E2E")
f.pack(side=RIGHT)

ws.mainloop()

In the above example, the canvas.create_rectangle() is used to create rectangle pieces on canvas.

Output :

By using the above code, we will get an output with a canvas having a table with 3 rows and 3 columns. And it is also having a grid view inside a table that helps to separate rows and columns.

Python Tkinter Table canvas
Python Tkinter Table Canvas

Read: Python Tkinter Scrollbar

Python Tkinter Table Sort

In this section, we will learn how to add a sort filter on Table in Python Tkinter.

So, we are learning about table sort and first, we need to understand what is sort?
In Python, sort is a function that is used to rearrange the values in form of ascending or descending order. But here, we are talking about sorting table data and we will be explaining how column sort works in the Tkinter table.

Example:

So, here we have given an example in which we will be able to arrange the columns of a table using drag and drop.

import tkinter as tk
from tkinter import Tk, ttk

def down(event):
    global cols_from, dx, cols_from_id
    db= event.widget
    if db.identify_region(event.x, event.y) != 'separator':
        cols = db.identify_column(event.x)
        cols_from_id =db.column(cols, 'id')
        cols_from = int(cols[1:]) - 1  
      
        bbox = db.bbox(db.get_children("")[0], cols_from_id)
        dx = bbox[0] - event.x  
        db.heading(cols_from_id, text='')
        visual_drag.configure(displaycolumns=[cols_from_id])
        visual_drag.place(in_=db, x=bbox[0], y=0, anchor='nw', width=bbox[2], relheight=1)
    else:
        cols_from = None

def BUP(event):
    db = event.widget
    cols_to = int(db.identify_column(event.x)[1:]) - 1  
    visual_drag.place_forget()
    if cols_from is not None:
        db.heading(cols_from_id, text=visual_drag.heading('#1', 'text'))
        if cols_from != cols_to:
            Tcols = list(db["displaycolumns"])
            if Tcols[0] == "#all":
                Tcols = list(db["columns"])

            if cols_from > cols_to:
                Tcols.insert(cols_to, Tcols[col_from])
                Tcols.pop(cols_from + 1)
            else:
                Tcols.insert(cols_to + 1, Tcols[cols_from])
                Tcols.pop(cols_from)
            db.config(displaycolumns=Tcols)

def BMotion(event):
    
    if visual_drag.winfo_ismapped():
        visual_drag.place_configure(x=dx + event.x)



col_from = 0

ws= Tk()


columns = ["D", "C", "B", "A"]

sort = ttk.Treeview(ws, columns=columns, show='headings') 

visual_drag = ttk.Treeview(ws, columns=columns, show='headings')

for cols in columns:
    sort.heading(cols, text=cols)
    visual_drag.heading(cols, text=cols)


for i in range(10):
    sort.insert('', 'end', iid='line%i' % i,
                values=(i+50, i+40, i+30, i+20, i+10))
    visual_drag.insert('', 'end', iid='line%i' % i,
                       values=(i+50, i+40, i+30, i+20, i+10))

sort.grid()
sort.bind("<ButtonPress>", down)
sort.bind("<ButtonRelease>",BUP)
sort.bind("<Motion>",BMotion)

ws.mainloop()

In the example, we have generated 4 labels with alphabets ‘A‘, ‘B‘, ‘C‘, and ‘D‘ with some values. But columns are not arranged properly. So, by using a drag method, we would be able to sort the columns as we required.

READ:  How to Remove All Non-numeric Characters in Pandas [4 Methods]

Output:

After running the following code, we come up with this output where we can see that the columns are not arranged in order. As column C values should come after column D. So, here we just drag the column through mouse hover and re-arrange the following in ascending order.

Python Tkinter Table sort
Unsorted Table

We can see in this figure that label ‘C‘ is swapped with label ‘D‘ and the columns are sorted now.

Python Tkinter Table with unsorted data
One Sorted Column

As we can see in this figure, all the columns are sorted in order and arranged properly.

Python Tkinter Table sorted
Sorted Table

Read: Python Tkinter Text Box Widget

Python Tkinter Editable Table

In this section, we will learn how to edit data inside a table in Python Tkinter.

  • In the below code, we have taken labels and buttons in which buttons are functioning the role of selection and editing of the records inside a table.
  • By Editing, we mean any data which was filled incorrectly by a user and wants to update that data again in the records with the help of the edit function. And we can use the edit option to make changes to records and makes our information correct.

Here comes the code that helps to explain the data editing inside a table.

Code:

from tkinter import *
from  tkinter import ttk


ws  = Tk()
ws.title('PythonGuides')
ws.geometry('500x500')
ws['bg'] = '#AC99F2'

game_frame = Frame(ws)
game_frame.pack()

#scrollbar
game_scroll = Scrollbar(game_frame)
game_scroll.pack(side=RIGHT, fill=Y)

game_scroll = Scrollbar(game_frame,orient='horizontal')
game_scroll.pack(side= BOTTOM,fill=X)

my_game = ttk.Treeview(game_frame,yscrollcommand=game_scroll.set, xscrollcommand =game_scroll.set)


my_game.pack()

game_scroll.config(command=my_game.yview)
game_scroll.config(command=my_game.xview)

#define our column
 
my_game['columns'] = ('player_Name', 'player_Country', 'player_Medal')

# format our column
my_game.column("#0", width=0,  stretch=NO)
my_game.column("player_Name",anchor=CENTER, width=80)
my_game.column("player_Country",anchor=CENTER,width=80)
my_game.column("player_Medal",anchor=CENTER,width=80)


#Create Headings 
my_game.heading("#0",text="",anchor=CENTER)
my_game.heading("player_Name",text="Id",anchor=CENTER)
my_game.heading("player_Country",text="Name",anchor=CENTER)
my_game.heading("player_Medal",text="Rank",anchor=CENTER)


#add data 
my_game.insert(parent='',index='end',iid=0,text='',
values=('Tom','US','Gold'))
my_game.insert(parent='',index='end',iid=1,text='',
values=('Aandrew','Australia','NA'))
my_game.insert(parent='',index='end',iid=2,text='',
values=('Anglina','Argentina','Silver'))
my_game.insert(parent='',index='end',iid=3,text='',
values=('Shang-Chi','China','Bronze'))


my_game.pack()

frame = Frame(ws)
frame.pack(pady=20)

#labels
playerid= Label(frame,text = "player_id")
playerid.grid(row=0,column=0 )

playername = Label(frame,text="player_name")
playername.grid(row=0,column=1)

playerrank = Label(frame,text="Player_rank")
playerrank.grid(row=0,column=2)

#Entry boxes
playerid_entry= Entry(frame)
playerid_entry.grid(row= 1, column=0)

playername_entry = Entry(frame)
playername_entry.grid(row=1,column=1)

playerrank_entry = Entry(frame)
playerrank_entry.grid(row=1,column=2)

#Select Record
def select_record():
    #clear entry boxes
    playerid_entry.delete(0,END)
    playername_entry.delete(0,END)
    playerrank_entry.delete(0,END)
    
    #grab record
    selected=my_game.focus()
    #grab record values
    values = my_game.item(selected,'values')
    #temp_label.config(text=selected)

    #output to entry boxes
    playerid_entry.insert(0,values[0])
    playername_entry.insert(0,values[1])
    playerrank_entry.insert(0,values[2])

#save Record
def update_record():
    selected=my_game.focus()
    #save new data 
    my_game.item(selected,text="",values=(playerid_entry.get(),playername_entry.get(),playerrank_entry.get()))
    
   #clear entry boxes
    playerid_entry.delete(0,END)
    playername_entry.delete(0,END)
    playerrank_entry.delete(0,END)

#Buttons
select_button = Button(ws,text="Select Record", command=select_record)
select_button.pack(pady =10)

edit_button = Button(ws,text="Edit ",command=update_record)
edit_button.pack(pady = 10)

temp_label =Label(ws,text="")
temp_label.pack()

ws.mainloop()

After running the above code, we have the following output in which displays some records of data. Also, it has two buttons that are used to select and edit a record that was filled incorrectly.

Python Tkinter Table Edittable
Python Tkinter Editable Table

In the given image, it is showing us how a user can select or edit data in records inside a table.

Python Tkinter Table Edidtable example
Select record

The below image is showing us the record that is edited and updates inside a table data.

Python Tkinter Table edittable output
Edited Record

You may also like to read the following articles.

So, in this tutorial, we discussed Python Table Tkinter and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python Tkinter Table with Scrollbar
  • Python Tkinter Table Widget
  • Python Tkinter Table Input
  • Python Tkinter Table List
  • Python Tkinter Table Refresh
  • Python Tkinter Table Canvas
  • Python Tkinter Table Sort
  • Python Tkinter Editable Table
  • Python Tkinter Table