Python Tkinter – How to display a table editor in a text widget?
In this article, we will discuss how to display a table editor in a text widget.
Before moving ahead let’s create a simple text widget first with the help of Tkinter. For the link to the Excel file used in the code click here.
Text Widget
The Text widget is used to show the text editor in the Tkinter window. A user can modify the text in the text editor
Python3
# Import tkinterfrom tkinter import * # Create an instance of tkinter windowwindow = Tk() # Size of the tkinter windowwindow.geometry("200x200") # Create text widgettext = Text(window, width=15, height=2) # insert text into the text fieldtext.insert(INSERT, "Hello world!") # Set the position of the widgettext.grid(row = 0,column = 0) window.mainloop() |
Output:
Example 1:
In this example, we are loading tabular data of rows and columns and iterating through the number of rows and columns. After that text is inserted into the text widget and the data table is displayed.
Python3
# import required modulesfrom tkinter import *import pandas as pd # Create an instance of tkinter framewindow = Tk() # Set the size of the tkinter windowwindow.geometry("300x200") # Load data from sourcedf = pd.read_excel("data.xlsx") # Extract number of rows and columnsn_rows = df.shape[0]n_cols = df.shape[1] # Extracting columns from the data and# creating text widget with some# background colorcolumn_names = df.columnsi=0for j, col in enumerate(column_names): text = Text(window, width=16, height=1, bg = "#9BC2E6") text.grid(row=i,column=j) text.insert(INSERT, col) # adding all the other rows into the gridfor i in range(n_rows): for j in range(n_cols): text = Text(window, width=16, height=1) text.grid(row=i+1,column=j) text.insert(INSERT, df.loc[i][j]) window.mainloop() |
Output:
Example 2:
Let’s use the same data and we will modify the country UK to the United Kingdom and save the new_data in excel format.
Python3
# import required modulesfrom tkinter import *import pandas as pd # Create an instance of tkinter framewindow = Tk() # Set the size of the tkinter windowwindow.geometry("300x200") # Load data from sourcedf = pd.read_excel("data.xlsx") # Extract number of rows and columnsn_rows = df.shape[0]n_cols = df.shape[1] # Extracting columns from the data and #creating text widget with some# background colorcolumn_names = df.columnsi=0for j, col in enumerate(column_names): text = Text(window, width=16, height=1, bg = "#9BC2E6") text.grid(row=i,column=j) text.insert(INSERT, col) # Dictionary for storing the text widget# referencescells = {} # adding all the other rows into the gridfor i in range(n_rows): for j in range(n_cols): text = Text(window, width=16, height=1) text.grid(row=i+1,column=j) text.insert(INSERT, df.loc[i][j]) cells[(i,j)] = text def do_something(): """ When user clicks the "Save" button, modified data will be saved in excel file """ for i in range(n_rows): for j in range(n_cols): if df.loc[i][j] != cells[(i,j)].get("1.0", "end-1c"): df.loc[[i],column_names[j]] = cells[(i,j)].get("1.0", "end-1c") df.to_excel("new_data.xlsx") save_button = Button( window, height = 2, width = 16, text ="Save", command = lambda:do_something())save_button.grid(row=7,column = 0)window.mainloop() |
Output:
Click on the Save button. When clicking on the save button a”new_data.xlsx” will be created with the modified data in the same directory.





