Tk - Entry Widgets

Entry widgets are used to accept a single line of text as input. Getting user input is almost mandatory in all Tk applications. The syntax for entry widget is shown below −

entry entryName options

Options

The options available for the entry widget are listed below in the following 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

-pady number

Sets the pady for the widget.

6

-relief condition

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

7

-textvariable varName

Variable associated with the widget. When the text of widget changes, the variable is set to text of widget.

8

-width number

Sets the width for widget.

9

-justify side

Sets the justification side. The valid sides are left and right.

10

-show character

Sets the character for secure entry.

A simple example using entry widget is shown below −

#!/usr/bin/wish

grid [entry .myEntry -background red -foreground white -relief ridge -borderwidth 8
   -font {Helvetica -18 bold} -width 35 -textvariable myvariable -justify right ]
set myvariable "Hello World"

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

An example for secure entry is shown below −

#!/usr/bin/wish

grid [entry .myEntry -background red -foreground white  -relief ridge -borderwidth 8
   -font {Helvetica -18 bold} -width 35 -textvariable myvariable -justify left -show "*"]
set myvariable "Hello World"

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