Python Tkinter Text

The Text widget is used to show the text data on the Python application. However, Tkinter provides us the Entry widget which is used to implement the single line text box.

The Text widget is used to display the multi-line formatted text with various styles and attributes. The Text widget is mostly used to provide the text editor to the user.

The Text widget also facilitates us to use the marks and tabs to locate the specific sections of the Text. We can also use the windows and images with the Text as it can also be used to display the formatted text.

The syntax to use the Text widget is given below.

Syntax

A list of possible options that can be used with the Text widget is given below.

SNOptionDescription
1bgThe background color of the widget.
2bdIt represents the border width of the widget.
3cursorThe mouse pointer is changed to the specified cursor type, i.e. arrow, dot, etc.
4exportselectionThe selected text is exported to the selection in the window manager. We can set this to 0 if we don't want the text to be exported.
5fontThe font type of the text.
6fgThe text color of the widget.
7heightThe vertical dimension of the widget in lines.
8highlightbackgroundThe highlightcolor when the widget doesn't has the focus.
9highlightthicknessThe thickness of the focus highlight. The default value is 1.
10highlighcolorThe color of the focus highlight when the widget has the focus.
11insertbackgroundIt represents the color of the insertion cursor.
12insertborderwidthIt represents the width of the border around the cursor. The default is 0.
13insertofftimeThe time amount in Milliseconds during which the insertion cursor is off in the blink cycle.
14insertontimeThe time amount in Milliseconds during which the insertion cursor is on in the blink cycle.
15insertwidthIt represents the width of the insertion cursor.
16padxThe horizontal padding of the widget.
17padyThe vertical padding of the widget.
18reliefThe type of the border. The default is SUNKEN.
19selectbackgroundThe background color of the selected text.
20selectborderwidthThe width of the border around the selected text.
21spacing1It specifies the amount of vertical space given above each line of the text. The default is 0.
22spacing2This option specifies how much extra vertical space to add between displayed lines of text when a logical line wraps. The default is 0.
23spacing3It specifies the amount of vertical space to insert below each line of the text.
24stateIt the state is set to DISABLED, the widget becomes unresponsive to the mouse and keyboard unresponsive.
25tabsThis option controls how the tab character is used to position the text.
26widthIt represents the width of the widget in characters.
27wrapThis option is used to wrap the wider lines into multiple lines. Set this option to the WORD to wrap the lines after the word that fit into the available space. The default value is CHAR which breaks the line which gets too wider at any character.
28xscrollcommandTo make the Text widget horizontally scrollable, we can set this option to the set() method of Scrollbar widget.
29yscrollcommandTo make the Text widget vertically scrollable, we can set this option to the set() method of Scrollbar widget.

Methods

We can use the following methods with the Text widget.

SNMethodDescription
1delete(startindex, endindex)This method is used to delete the characters of the specified range.
2get(startindex, endindex)It returns the characters present in the specified range.
3index(index)It is used to get the absolute index of the specified index.
4insert(index, string)It is used to insert the specified string at the given index.
5see(index)It returns a boolean value true or false depending upon whether the text at the specified index is visible or not.

Mark handling methods

Marks are used to bookmark the specified position between the characters of the associated text.

SNMethodDescription
1index(mark)It is used to get the index of the specified mark.
2mark_gravity(mark, gravity)It is used to get the gravity of the given mark.
3mark_names()It is used to get all the marks present in the Text widget.
4mark_set(mark, index)It is used to inform a new position of the given mark.
5mark_unset(mark)It is used to remove the given mark from the text.

Tag handling methods

The tags are the names given to the separate areas of the text. The tags are used to configure the different areas of the text separately. The list of tag-handling methods along with the description is given below.

SNMethodDescription
1tag_add(tagname, startindex, endindex)This method is used to tag the string present in the specified range.
2tag_configThis method is used to configure the tag properties.
3tag_delete(tagname)This method is used to delete a given tag.
4tag_remove(tagname, startindex, endindex)This method is used to remove a tag from the specified range.

Example

Output:

Python Tkinter Text



Latest Courses