Python Tkinter Text

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:

OPTION DESCRIPTION
bg This option helps us to set the normal background colour of the widget.
bd This option is used to set the size of the border around the widget.
cursor This option helps us to set the style of cursor like an arrow, dot etc
exportselection This option is used to export selected text to selection in the window manager.
font This option is used to set font type in the widget.
fg This option helps us to set the foreground colour of the widget.
height This option helps us to set the height of the widget.
highlightbackground This option is used to set the highlight colour when the widget has no focus.
highlightcolor This option is used to set the highlight colour when the widget has focus.
highlightthickness This option is used to set the thickness of the highlight focus. By default highlightthickness=1.
insertbackground This option is used to set the colour of the insertion cursor. By default insertbackground=black.
insertborderwidth This option is used to set the width of the border around the insertion cursor. By default insertborderwidth=0.
insertofftime This option is used to set time around in which during which the insertion cursor is in off blink cycle. By default inserontime=300 milliseconds.
insertontime This option is used to set time around in which during which the insertion cursor is in on blink cycle. By default inserontime=600 milliseconds.
insertwidth This option is used to set the width of the insertion cursor.By default insertwidth= 2.
padx This option helps us to set the space left and right.
pady This option helps us to set space above and below.
relief This helps us to set the 3D style text which is by default is relief=SUNKEN.
selectbackground This option is used to set the background colour of selected text.
selectborderwidth This option is used to set the border of the width around the selected text.
spacing1 This option is used to set the vertical space above the lines.
spacing2 This option is used to set vertical space between the lines when a logical line wraps.
spacing3 This option is used to set vertical space below the text.
state We can makeout widget disable It will not respond to mouse or keyword events just set state=DISABLED.
tabs This option helps us to the position of tab characters.
width This helps us to set the width of the widget.
wrap This option helps us to wrap up the line lines with so much text into multiple lines.
xscrollcommand This option used to let the user scroll the Text horizontally.
yscrollcommand This option used to let the user scroll the Text vertically.

Python Tkinter Text Methods:

After learning about various available options in Python Tkinter Text Its time to check out some available methods for Tkinter Text widget.

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