In this tutorial, we will learn about Python Tkinter Text. The Tkinter Text widget is used to show multi-line text in the application. The best thing about this Text widget we can set the style of text in this widget such as we can change colour and font-style. We can also use the windows and images with the Text. Check out the following syntax example to know how we can add a text widget in our Python application.

Syntax to add Python Tkinter Text:

w = Text (master, options) 
  • master: This represents the parent window.
  • options: Here is the list of most commonly used options for this widget.

Example Program of Python Tkinter Text:

# importing tkinter lib
from tkinter import *  
    
mainWindow = Tk()  

mainWindow.geometry("320x180")

text = Text(mainWindow, height = 5, width = 35) 
   
lable = Label(mainWindow, text = "Owlbuddy.com") 
lable.config(font =("Arial", 14)) 
   
text.insert(END, "Learn Programming languages here..") 
text.tag_add("style", "1.0", "1.50")  
text.tag_config("style", background="skyblue", foreground="black")  

lable.pack()
text.pack() 
  
mainWindow.mainloop() 

Various possible options in Python Tkinter Text:

python tkinter text

Various possible options in Python Tkinter Text:

OPTIONDESCRIPTION
bgThis option helps us to set the normal background colour of the widget.
bdThis option is used to set the size of the border around the widget.
cursorThis option helps us to set the style of cursor like an arrow, dot etc
exportselectionThis option is used to export selected text to selection in the window manager.
fontThis option is used to set font type in the widget.
fgThis option helps us to set the foreground colour of the widget.
heightThis option helps us to set the height of the widget.
highlightbackgroundThis option is used to set the highlight colour when the widget has no focus.
highlightcolorThis option is used to set the highlight colour when the widget has focus.
highlightthicknessThis option is used to set the thickness of the highlight focus. By default highlightthickness=1.
insertbackgroundThis option is used to set the colour of the insertion cursor. By default insertbackground=black.
insertborderwidthThis option is used to set the width of the border around the insertion cursor. By default insertborderwidth=0.
insertofftimeThis option is used to set time around in which during which the insertion cursor is in off blink cycle. By default inserontime=300 milliseconds.
insertontimeThis option is used to set time around in which during which the insertion cursor is in on blink cycle. By default inserontime=600 milliseconds.
insertwidthThis option is used to set the width of the insertion cursor.By default insertwidth= 2.
padxThis option helps us to set the space left and right.
padyThis option helps us to set space above and below.
reliefThis helps us to set the 3D style text which is by default is relief=SUNKEN.
selectbackgroundThis option is used to set the background colour of selected text.
selectborderwidthThis option is used to set the border of the width around the selected text.
spacing1This option is used to set the vertical space above the lines.
spacing2This option is used to set vertical space between the lines when a logical line wraps.
spacing3This option is used to set vertical space below the text.
stateWe can makeout widget disable It will not respond to mouse or keyword events just set state=DISABLED.
tabsThis option helps us to the position of tab characters.
widthThis helps us to set the width of the widget.
wrapThis option helps us to wrap up the line lines with so much text into multiple lines.
xscrollcommandThis option used to let the user scroll the Text horizontally.
yscrollcommandThis option used to let the user scroll the Text vertically.

Python Tkinter Text Functions:

FunctionDescription
delete(startindex [,endindex])This function used to delete all the characters between specified index numbers.
get(startindex [,endindex])This function returns all the characters between specified index numbers.
index(index)This function is used to get the absolute index of the specified index.
insert(index [,string]…)This function is used to set the specified string at the specified index.
see(index)This function returns true or false after checking the text at specified location visible or not.