Python Tkinter Text Widget
In this post, you will learn about the Tkinter text widget of the Python programming language with implementation process.
Tkinter is a standard cross-platform package for creating graphical user interfaces (GUIs). It is also called the Tk interface. Tkinter comes with a wide range of necessary widgets for almost all common tasks, like text, buttons, radio buttons, checkboxes, scales, labels, and much more. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. In this article, you will learn about the Tkinter Text widget. The Text Widget can be used for many applications where multi-line text is required, like messaging, displaying, or sending information.
Displaying text on a widget does not only mean displaying plain text; it should be capable of multiple styles and fonts, embedded images, and so on. The Python Tkinter text widget is feature-rich, it has multiple methods and options. In this article, we have mentioned some of the possible styles and embedded objects.
Python Tkinter Text Widget
Here is the syntax of text widget.
T = Text(root,options)
Options | Descriptions |
root | The root window |
bg | The background color |
fg | The foreground color |
bd | The border of widget. |
height | The height of the widget. |
width | The width of the widget. |
font | The font type of the text. |
cursor | The type of the cursor to be used. |
highlighcolor | The color of the focus highlight. |
insetofftime | The time in milliseconds for which the cursor blink is off. |
insertontime | The time in milliseconds for which the cursor blink is on. |
insertwidth | The width of the insertion cursor. |
padx | The horizontal padding of the widget. |
pady | The vertical padding of the widget. |
relief | The border type which can be SUNKEN, RAISED, GROOVE and RIDGE. |
yscrollcommand | To make the widget vertically scrollable. |
xscrollcommand | To make the widget horizontally scrollable. |
Some Commonly Methods of Text widget
The text widget has three distinct helper methods- Index, Mark and Tag.
Methods | Descriptions |
delete(startindex, endindex) | It is used to delete the characters of the specified range. |
get(startindex, endindex) | It returns the characters present in the specified range. |
index(index) | To get the specified index. |
insert(index) | To insert a string at a specified index. |
see(index) | It returns true if the text located at the index position is visible. |
Tkinter Text Mark Handling Methods
Marks are used to bookmark the specified positions between two characters within a given text.
Methods | Descriptions |
index(mark) | To get the index of the specified mark. |
mark_gravity(mark, gravity) | To get the gravity of the given mark. |
mark_names() | To get all the marks present in the Text widget. |
mark_set(mark, index) | To inform a new position of the given mark. |
mark_unset(mark) | To remove the given mark from the text. |
Tkinter Text Widget Tag Handling Methods
The each separate area of text is called a tag and the name given to the area is called a tagname. These are the lists of tag handling methods along with descriptions.
Methods | Descriptions |
tag_add(tagname, startindex, endindex) | To tag the string present in the specified range |
tag_config | To configure the tag properties |
tag_delete(tagname) | To delete a given tag |
tag_remove(tagname, startindex, endindex) | To remove a tag from the specified range |
Tkinter Text Widget Examples
Here is a very simple example of a text widget. You can see how we set the height and width of the text widget, background colour, font size of the text.
import tkinter as tk
root = tk.Tk()
#Set the size of window
root.geometry("350x200")
T = tk.Text(root, height=20, width=40,
bg = "light cyan")
T.config(font =("Courier", 13))
T.insert(tk.END, "Welcome to etutorialspoint\nTkinter Text Widget\n")
T.pack()
tk.mainloop()
When you will execute the above code, it returns the following output -
Tkinter Get User Input
We can use the Tkinter to get user input. Here is a basic example to get and display the user input in a text widget.
import tkinter as tk
root = tk.Tk()
#Define Function
def get_input():
name = inputtxt.get("1.0","end-1c")
print(name)
output.insert(tk.END,name)
#Set the size of window
root.geometry("400x300")
ques = tk.Label(text = "What is your name?")
inputtxt = tk.Text(root, height = 5,
width = 25,
bg = "light yellow")
output = tk.Text(root, height = 3,
width = 25)
btn = tk.Button(root, height = 1,
width = 20,
text ="Show", bg = "blue",
command = lambda:get_input())
ques.pack()
inputtxt.pack()
btn.pack()
output.pack()
tk.mainloop()
Tkinter Text Widget with Image
The PhotoImage class is used to display images in a text widget. This accepts the image path as an argument and returns the image object.
import tkinter as tk
root = tk.Tk()
root.geometry("600x200")
IM = tk.Text(root, height=10, width=30)
IM.pack(padx = 5, pady = 5)
img = tk.PhotoImage(file = "flower.gif")
IM.image_create(tk.END, image = img)
text = tk.Text(root, height=10, width=28, bg='cyan')
text.config(font =("Courier", 13))
text.insert(tk.END, "Welcome to etutorialspoint\nTkinter Text Widget\n")
IM.pack(side=tk.LEFT)
text.pack(side=tk.LEFT , padx = 5, pady = 5)
root.mainloop()
Related Articles
Python program to multiply two numbersMultiply all elements in list Python
Python program to map two lists into a dictionary
Eye Detection Program in Python OpenCV
Python OpenCV Histogram of Grayscale Image
How to find the stop words in nltk Python
Python Tkinter Combobox Event Binding
Python Tkinter Combobox
Add background image in Python Tkinter
Python Tkinter Text Widget
Countdown clock and timer using Tkinter in Python
Python Tkinter Frame Widget
Python Tkinter Checkbutton Widget
Entry Field Validation in Tkinter Python
Python Tkinter Tutorial with Examples
Python Tkinter Scale Widget
Python3 Tkinter Messagebox
Python 3 Tkinter Menu Bar
Python Tkinter Geometry Managers
Python get visitor information by IP address
Python OpenCV ColorMap
Python gmplot to add google map on a web page