home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

18.8 The Entry Widget

Create an entry widget with the Entry method. The value that the user has typed into the widget is stored in the variable pointed to by the -textvariable option.

$parent->Entry (
The standard configuration options that apply to Entry are: -background , -bg , -borderwidth , -bd , -cursor , -exportselection , -font , -foreground , -fg , -highlightbackground , -highlightcolor , -highlightthickness , -insertbackground , -insertborderwidth , -insertofftime , -insertontime , -insertwidth , -justify , -relief , -selectbackground , -selectborderwidth , -selectforeground , -state , -takefocus , and -xscrollcommand .

Other options are:

-show => x

Defines a character to be displayed in place of actual typed text (for use with passwords).

-textvariable => \$variable

Points to the variable containing text to be displayed in the entry widget. Button text will change as $variable does.

18.8.1 Text Indexes

In an Entry widget, several indexes are defined to identify positions in the entry text, for use by the methods used for retrieving or manipulating entry text. These indexes are:


An integer representing a character position, with 0 as the first character in the string.


The character directly after the insert cursor.


The first character in the selection block.


The character after the last character in the selection block.


The anchored position.


The position just after the last character in the entry string.

@ x

The character containing the specified x coordinate.

18.8.2 Entry Methods

In addition to configure and cget , the following methods are supported for the Entry widget:


Deletes text from the widget. For example, to delete the selected text:

$entry->delete('sel.first', 'sel.last');


Gets the contents of the entry widget. For example:

$input = $entry->get;


Places the cursor at the specified index. For example, to move the cursor to the very end of the entry string:



Converts a named index into a numeric one.

$length = $entry->index('end');


Inserts text at the specified index. For example, to append the ".txt" string to the end of the entry string:

$entry -> insert('end', '.txt');


Manipulates the selected block. The first argument can be any of:


Extends selected text to the index specified in the second argument.

$entry->selection('adjust', 'end');


Clears the selection block.



Resets the "anchor" index to the index specified in the second argument.



Determines if any text is currently selected.

if ($entry->selection('present')) {


Changes the selection range to the indexes specified in the second and third arguments. For example, to change the selection to include the entire entry string:



Extends the selection from the current anchor position to the specified index.



Manipulates the text in view. With no arguments, returns a list of two numbers between 0 and 1, defining what portion of the entry text is currently hidden on the left and right sides, respectively. With arguments, the function of xview changes:


If the first argument is an index, that position becomes the leftmost position in view. For example, to reset the visible portion to the beginning of the string:



Moves the specified fraction of the entry text to the left of the visible portion. For example, to hide the first 10% of the entry text:



Scrolls the text left or right by the specified number of units (characters, in this context) or pages. Used mainly as a callback to a scrollbar; pressing on an arrow moves by units (characters), and pressing on the trough moves by pages. The number is either 1 or -1, to move forwards or backwards, respectively. For example: