Tk - Text Widget

Tk text widget is a general purpose editable text widget with features for multiple options. The syntax for text widget is shown below −

text textName options


The options available for the text widget are listed below in table −

Sr.No. Syntax & Description

-background color

Used to set background color for widget.


-borderwidth width

Used to draw with border in 3D effects.


-font fontDescriptor

Used to set font for widget.


-foreground color

Used to set foreground color for widget.


-relief condition

Sets the 3D relief for this widget. The condition may be raised, sunken, flat, ridge, solid, or groove.


-width number

Sets the width for widget.


-height number

Used to set height for widget.

A simple example for text widget is shown below −


grid [text .myText -background red -foreground white -relief ridge -borderwidth 8 -padx 10
   -pady 10 -font {Helvetica -18 bold} -width 20 -height 5]
.myText insert 1.0 "Hello\nWorld\n"
.myText insert end "A new line\n"
.myText tag configure para -spacing1 0.15i -spacing2 0.05i \
   -lmargin1 0.25i -lmargin2 0.2i -rmargin 0.25i
.myText tag configure hang -lmargin1 0.30i -lmargin2 0.25i
.myText tag add para 1.0 2.end
.myText tag add hang 3.0 3.end

When we run the above program, we will get the following output −

Text Widget Example

As you can see, text widgets works with the help of procedures like tag, insert, and delete. Most of the tag usages have been covered in the above example.

Kickstart Your Career

Get certified by completing the course

Get Started