NumEntry
A NumEntry widget is a basic Entry widget,
but is used to accept only integer values. Cases like this
often arise, such as ordering a certain quantity of
something or specifying the number of copies of a file
to print. The basic usage is
my $numentry = $mw->NumEntry( [ option => value ] );
with options as described below:
- -minvalue
This defines the minimum legal value that the widget can hold.
If this value is undefined, which is the default,
then there is no minimum value.
- -maxvalue
This defines the maximum legal value that the widget can hold.
If this value is undefined, which is the default,
then there is no maximum value.
- -bell
This is specified by a boolean value. If true, then a bell will
ring if the user attempts to enter an illegal character into the
entry widget, and when the user reaches the upper or lower limits
when using the up/down buttons for keys. the default is true.
- -textvariable
This is a reference to a scalar variable that contains the
value currently in the NumEntry. Use the variable only for
reading.
- -value
This specifies the value to be inserted into the entry widget.
It is similar to the standard -text option, but
a range check on the value will be performed.
A simple example of this widget appears below.
#!perl
# file numentry.pl
use Tk;
use Tk::NumEntry;
use strict;
use warnings;
my $message = 'You have selected nothing yet';
my $value = '';
my $mw = MainWindow->new;
$mw->title('NumEntry');
my $label = $mw->Label(-textvariable => \$message);
my $instruct = $mw->Label(-text => 'Please choose a year');
my $exit = $mw->Button(-text => 'Exit',
-command => [$mw => 'destroy']);
my $numentry = $mw->NumEntry(-minvalue => 1990,
-maxvalue => 2000,
-width => 8,
-value => 1995,
-textvariable => \$value);
my $show = $mw->Button(-text => 'Show value',
-command => sub{$message = "You have selected $value"});
$instruct->pack;
$numentry->pack;
$label->pack;
$show->pack;
$exit->pack;
MainLoop;
Here the value of the NumEntry widget is associated
with the variable $value, and the $show button
can be pressed to display the current value. This
window appears below.
Figure 3.25:
Example of a NumEntry widget
|
Randy Kobes
2003-11-17