Python Text Widget - Tkinter Text

Published December 16, 2021

This widget is used when a user wishes to enter many lines of text into an application. This widget is commonly used in the python apps that need the insertion and display of multiline text, such as chat applications and contact us sections. This widget also supports the insertion of other media files, such as photos.

In this tutorial, we will learn how to make Tkinter Text.

 

Syntax

w = Text (root, option, ... )

Where root denotes the parent root and option denotes the various option characteristics used to generate the widget.

Tkinter Text Widget Options

  • bg – specides background colour 

  • fg – defines foreground colour 

  • bd – specifies border  size of widget. 

  • height – specifies height of the widget. 

  • width – specifies width of the widget. 

  • font – specifies Font type of the text.

  • cursor – specifies the type of the cursor to be used. 

  • insetofftime – specifies the time in milliseconds for which the cursor blink is off. 

  • insertontime – specifies the time in milliseconds for which the cursor blink is on. 

  • padx – defines horizontal padding. 

  • pady – defines vertical padding. 

  • state – defines if the widget will be responsive to mouse or keyboards movements. 

  • highligththickness – defines the thickness of the focus highlight. 

  • insertionwidth – defines the width of insertion character. 

  • relief – type of the border which can be RIDGE, GROOVE, RAISED, SUNKEN. 

  • yscrollcommand – makes the widget vertically scrollable. 

  • xscrollcommand – makes the widget horizontally scrollable. 

 

 

Tkinter Text Widget Examples

from tkinter import *

def onclick():pass

root = Tk()

text = Text(root)

text.insert(INSERT, "Here Comes")

text.insert(END, "the Best Programmer in Asia.....")

text.pack()

text.tag_add("here", "1.0", "1.4")

text.tag_add("start", "1.8", "1.13")

text.tag_config("here", background="green", foreground="red")

text.tag_config("start", background="yellow", foreground="blue")

root.mainloop()

 

 

Output:

Python Text Tikinter example

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

295 Views