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 tkinter from tkinter import * # Create an instance of tkinter window window = Tk() # Size of the tkinter window window.geometry( "200x200" ) # Create text widget text = Text(window, width = 15 , height = 2 ) # insert text into the text field text.insert(INSERT, "Hello world!" ) # Set the position of the widget text.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 modules from tkinter import * import pandas as pd # Create an instance of tkinter frame window = Tk() # Set the size of the tkinter window window.geometry( "300x200" ) # Load data from source df = pd.read_excel( "data.xlsx" ) # Extract number of rows and columns n_rows = df.shape[ 0 ] n_cols = df.shape[ 1 ] # Extracting columns from the data and # creating text widget with some # background color column_names = df.columns i = 0 for 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 grid for 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 modules from tkinter import * import pandas as pd # Create an instance of tkinter frame window = Tk() # Set the size of the tkinter window window.geometry( "300x200" ) # Load data from source df = pd.read_excel( "data.xlsx" ) # Extract number of rows and columns n_rows = df.shape[ 0 ] n_cols = df.shape[ 1 ] # Extracting columns from the data and #creating text widget with some # background color column_names = df.columns i = 0 for 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 # references cells = {} # adding all the other rows into the grid for 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.
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.