Perl in a Nutshell

Perl in a NutshellSearch this book
Previous: 18.7 The Label WidgetChapter 18
Perl/Tk
Next: 18.9 The Scrollbar Widget
 

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 (options)
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:

n

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

insert

The character directly after the insert cursor.

sel.first

The first character in the selection block.

sel.last

The character after the last character in the selection block.

anchor

The anchored position.

end

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:

delete

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

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

get

Gets the contents of the entry widget. For example:

$input = $entry->get;

icursor

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

$entry->icursor('end');

index

Converts a named index into a numeric one.

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

insert

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

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

selection

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

adjust

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

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

clear

Clears the selection block.

$entry->selection('clear');

from

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

$entry->selection('from',0);

present

Determines if any text is currently selected.

if ($entry->selection('present')) {
	$entry->delete('sel.first','sel.last');
}

range

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:

$entry->selection('range',0,'end');

to

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

$entry->selection('to','insert');

xview

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:

index

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:

$entry->xview(0);

moveto

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:

$entry->xview('moveto',0.1);

scroll

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:

$entry->xview('scroll',1,'units');


Previous: 18.7 The Label WidgetPerl in a NutshellNext: 18.9 The Scrollbar Widget
18.7 The Label WidgetBook Index18.9 The Scrollbar Widget