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

Options

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

Sr.No. Syntax & Description
1

-background color

Used to set background color for widget.

2

-borderwidth width

Used to draw with border in 3D effects.

3

-font fontDescriptor

Used to set font for widget.

4

-foreground color

Used to set foreground color for widget.

5

-relief condition

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

6

-width number

Sets the width for widget.

7

-height number

Used to set height for widget.

A simple example for text widget is shown below −

#!/usr/bin/wish

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.

tk_basic_widgets.htm
Advertisements