Add advanced features to a tkinter Text widget


Tkinter text widget is much more than just a multiline text editor. It is useful for many applications where user interaction is required.

In order to configure the text widget, tkinter provides many functions and methods. Some of the features that we can add to style the text widgets are: changing the font-family, background-color, foreground-color, adding and selecting a particular text, changing font-size, and many more.

Example

In this example, we will see how we can add different features to a particular text widget,

#Import tkinter library
from tkinter import *

#Create an instance of tkinter frame
win = Tk()

#Set the geometry of tkinter window
win.geometry("750x250")

#Create a text widget
text= Text(win, width= 50, height=14)

#Set default text for text widget
text.insert(INSERT, "Tkinter is a Python Library which is used to create various types of applications.")
text.insert(END, "Learning Tkinter is Awesome!!")

#Select Text by adding tags
text.tag_add("start", "1.0","1.7")
text.tag_configure("start", background= "OliveDrab1", foreground= "black")

#Configure the Text Style
text.configure(font=('Times New Roman',15))
text.pack()
win.mainloop()

Output

Running the above code will display a window containing a text widget with some features and properties.

Updated on: 15-Apr-2021

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements