TEXT IN TKINTER
In this tutorial, we are going to learn about the text widget in tkinter. We will learn about how we can apply different types of text methods along with tags and marks.
Recommended Book: Python GUI Programming with Tkinter
What is Text in Tkinter?
Text widgets in Tkinter are used to provide options to use text on Python application on multiple lines; However, we already have the option on Entry widget which is used to implement single lines. With the text widget we can change the font and color to be displayed.
Text widget also abets to use marks and tabs for locating different areas of the text. Use of windows and images along with the Text allows us to display the formatted text. Syntax of the text widget is:
text_tk = Text ( window, features )
Text features and properties are:
1 | bg | Displays the background color of the widget. |
2 | bd | It shows the width of the border. Same as borderwidth. |
3 | cursor | Shows the type of cursor when the mouse is hovered over the widget. The default is a pointed arrow cursor. |
4 | exportselection | The text selected under this option is exported in the window manager. We can set it to 0 for not exporting anything. |
5 | font | It shows the font of the widget. |
6 | fg | Determines the foreground color of font that is used for the text widget. |
7 | height | It represents the height of the text widget (number of lines). |
8 | highlightbackground | It shows the highlight color of the background of the text widget when it is clicked. |
9 | highlightthickness | Specifies the thickness of the highlight in focus. |
10 | highlightcolor | It shows the highlight color of the text widget when it is clicked. |
11 | insertbackground | Specifies the color of the insertion cursor. Black color is default. |
12 | insertborderwidth | Specifies the border width of the cursor of the cursor. Default value is 0. |
13 | insertofftime | Specifies the time in milliseconds in which the insertion cursor turns off during the blink cycle. |
14 | insertontime | Specifies the time in milliseconds in which the insertion cursor turns on during the blink cycle. |
15 | insertwidth | Specifies the width of the insertion cursor. |
16 | padx | Adds padding in the horizontal direction |
17 | pady | Adds padding in the vertical direction |
18 | relief | This displays different types of borders. By default, it has a FLAT border. |
19 | selectbackground | Sets the background color of the text widget. |
20 | selectborderwidth | Sets the border width of the text widget. |
21 | spacing1 | Demonstrates the amount of vertical space given above each line. Default value is 0 |
22 | spacing2 | Demonstrates the amount of extra vertical space given above each line. Default value is 0 |
23 | spacing3 | Demonstrates the amount of vertical space given below each line. |
24 | state | This represents DISABLED or ACTIVE state of the widget. |
25 | tabs | Controls how the tab character will be used for the positioning of the text. |
26 | width | Shows the width of the text characters. |
27 | wrap | This option is used for wrapping a line into multiple lines. |
28 | xscrollcommand | If you have a scrollable text widget, then xscrollcommand will be used as a .set() method for the horizontal scrollbar. |
29 | yscrollcommand | If you have a scrollable text widget, then xscrollcommand will be used as a .set() method for the vertical scrollbar |
Methods for a text widget are:
1 | delete(startindex, endindex) | This method is used to delete characters within the specified range. |
2 | get(startindex, endindex) | It fetches the characters available in the given range |
3 | index(index) | It fetches the absolute index of the specified index. |
4 | insert(index, string) | It is used for inserting a string through an index value. |
5 | see(index) | It returns a boolean value depending on whether the text specified at the given index is displayed or not. |
Mark methods:
Marks are of invisible nature, means a user cannot see them. They are positioned between the characters and move along with the text.
1 | index(mark) | It is used to fetch the index value at the specified mark. |
2 | mark_gravity(mark, gravity) | It is used to fetch the gravity at the provided mark. |
3 | mark_names() | It is used to get all the marks available in the widget. |
4 | mark_set(mark, index) | Specifies the new position of the provided mark. |
5 | mark_unset(mark) | Removes the provided mark of the text. |
Tags Methods:
Tags are used for displaying styles and calling events within the ranges of text.
1 | tag_add(tagname, startindex, endindex) | This method is used for tagging string within the given range. |
2 | tag_config | This method is used for configuring the tagging properties. |
3 | tag_delete(tagname) | This method is used to delete a provided tag. |
4 | tag_remove(tagname, startindex, endindex) | This method is used for removing a tag from the given range. |
Creating Text Widget
You can create simple a text widget as follows:
from tkinter import * screen = Tk() screen.geometry('400x400') text = Text(screen) text.insert(INSERT, "I am red") text.insert(END, " and I am blue") text.pack() text.tag_add("hg1", "1.5", "1.8") text.tag_add("hg2", "1.18", "1.23") text.tag_config("hg1", background="red", foreground="white") text.tag_config("hg2", background="blue", foreground="white") screen.mainloop()
Output will be:
References have been used from here