The Wayback Machine - https://web.archive.org/web/20240602200606/https://www.geeksforgeeks.org/python-tkinter-text-widget/
Open In App

Python Tkinter – Text Widget

Last Updated : 24 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs.
Note: For more information, refer to Python GUI – tkinter
 

Text Widget

Text Widget is used where a user wants to insert multiline text fields. This widget can be used for a variety of applications where the multiline text is required such as messaging, sending information or displaying information and many other tasks. We can insert media files such as images and links also in the Textwidget.
Syntax: 
 

T = Text(root, bg, fg, bd, height, width, font, ..)

Optional parameters 
 

  • root – root window. 
     
  • bg – background colour 
     
  • fg – foreground colour 
     
  • bd – border of widget. 
     
  • height – height of the widget. 
     
  • width – width of the widget. 
     
  • font – Font type of the text. 
     
  • cursor – The type of the cursor to be used. 
     
  • insetofftime – The time in milliseconds for which the cursor blink is off. 
     
  • insertontime – the time in milliseconds for which the cursor blink is on. 
     
  • padx – horizontal padding. 
     
  • pady – vertical padding. 
     
  • state – defines if the widget will be responsive to mouse or keyboards movements. 
     
  • highlightthickness – defines the thickness of the focus highlight. 
     
  • insertionwidth – defines the width of insertion character. 
     
  • relief – type of the border which can be SUNKEN, RAISED, GROOVE and RIDGE. 
     
  • yscrollcommand – to make the widget vertically scrollable. 
     
  • xscrollcommand – to make the widget horizontally scrollable. 
     

Some Common methods
 

  • index(index) – To get the specified index. 
     
  • insert(index) – To insert a string at a specified index. 
     
  • see(index) – Checks if a string is visible or not at a given index. 
     
  • get(startindex, endindex) – to get characters within a given range. 
     
  • delete(startindex, endindex) – deletes characters within specified range.

Tag handling methods 
 

  • tag_delete(tagname) – To delete a given tag. 
     
  • tag_add(tagname, startindex, endindex) – to tag the string in the specified range 
     
  • tag_remove(tagname, startindex, endindex) – to remove a tag from specified range 
     

Mark handling methods 
 

  • mark_names() – to get all the marks in the given range. 
     
  • index(mark) – to get index of a mark. 
     
  • mark_gravity() – to get the gravity of a given mark.
     

Example 1: 
 

Python3




import tkinter as tk
 
 
root = Tk()
 
# specify size of window.
root.geometry("250x170")
 
# Create text widget and specify size.
T = Text(root, height = 5, width = 52)
 
# Create label
l = Label(root, text = "Fact of the Day")
l.config(font =("Courier", 14))
 
Fact = """A man can be arrested in
Italy for wearing a skirt in public."""
 
# Create button for next text.
b1 = Button(root, text = "Next", )
 
# Create an Exit button.
b2 = Button(root, text = "Exit",
            command = root.destroy)
 
l.pack()
T.pack()
b1.pack()
b2.pack()
 
# Insert The Fact.
T.insert(tk.END, Fact)
 
tk.mainloop()


Output 
 

python-tkinter-text

Example 2: Saving Text and performing operations
 

Python3




from tkinter import *
 
root = Tk()
root.geometry("300x300")
root.title(" Q&A; ")
 
def Take_input():
    INPUT = inputtxt.get("1.0", "end-1c")
    print(INPUT)
    if(INPUT == "120"):
        Output.insert(END, 'Correct')
    else:
        Output.insert(END, "Wrong answer")
     
l = Label(text = "What is 24 * 5 ? ")
inputtxt = Text(root, height = 10,
                width = 25,
                bg = "light yellow")
 
Output = Text(root, height = 5,
              width = 25,
              bg = "light cyan")
 
Display = Button(root, height = 2,
                 width = 20,
                 text ="Show",
                 command = lambda:Take_input())
 
l.pack()
inputtxt.pack()
Display.pack()
Output.pack()
 
mainloop()


Output 
 

python-tkinter-text

 



Previous Article
Next Article

Similar Reads

How to Set Text of Tkinter Text Widget With a Button?
Prerequisite: Python GUI – tkinter Text Widget is used where a user wants to insert multi-line text fields. In this article, we are going to learn the approaches to set the text inside the text fields of the text widget with the help of a button. Approach: Using insert and delete methodImport the Tkinter module.Create a GUI window.Create our text w
2 min read
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 e
3 min read
Hide Python Tkinter Text widget border
Tkinter is a standard GUI library for Python, providing tools to create graphical user interfaces easily. When designing interfaces, developers often encounter the need to customize widget appearances, including removing widget borders. Borders can sometimes clash with the desired aesthetic or layout of the application. This article explores what w
3 min read
Tkinter | Adding style to the input text using ttk.Entry widget
Tkinter is a GUI (Graphical User Interface) module which is widely used to create GUI applications. It comes along with the Python itself. Entry widgets are used to get the entry from the user. It can be created as follows- entry = ttk.Entry(master, option = value, ...) Code #1: Creating Entry widget and taking input from user (taking only String d
2 min read
Create Find and Replace features in Tkinter Text Widget
Prerequisites: Python GUI – tkinterFirstly we need to find all the words or letters that we want to replace in our below text for that we will use the find function in which we will determine all the starting and the ending indexes from the editor of the same word after that we will apply the replace functionality in which we will delete that part
4 min read
Change the color of certain words in the tkinter text widget
Python has various options for Graphical User Interface (GUI) development. One of the options is Tkinter. Tkinter and Python together provide a faster way for GUI development. The Tk GUI toolkit provides an object-oriented interface. For creating GUI applications using Tkinter we have to follow a few steps – Import Tkinter module.Create the main wi
3 min read
How to Set the Default Text of Tkinter Entry Widget?
The Tkinter Entry widget does not have any text attribute that can be used to set the default text. Therefore, default text must be added to any text field in Tkinter using the following methods: Insert methodstringvar method Method 1: Using the insert method The tkinter module is imported. The root widget is created and this must be done before cr
2 min read
How to set the tab size in Text widget in Tkinter?
Prerequisite: Tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tki
2 min read
How to stop copy, paste, and backspace in text widget in tkinter?
In this article, we will see how to stop backspacing, copying, and pasting in the text widget in Tkinter. In order to disable copying, pasting, and backspacing in Tkinter a Text widget, you’ve to bind the event with an event handler, binding helps us to create complex GUI applications that bind some specific key to functions, which execute when tha
2 min read
How To Make The Tkinter Text Widget Read Only?
The Tkinter library in Python provides a powerful and flexible way to create GUI applications. One of the widgets available in Tkinter is the Text widget, which is used for displaying and editing multi-line text. However, there might be situations where you want the Text widget to be read-only, preventing users from modifying its content. This arti
3 min read
Practice Tags :