Hi All,
I try to validate the content of entry in Tk::LabEntry widget. I would like to change the configuration of the label part if the content is not a number (simply to color it red). I can do this with the background of the entry (code below), however an attempt to reach the option -labelBackground produces an error message "unknown option".
I know that Tk::LabEntry is a Mega-Widget. I cannot find a way to reach its configuration (that is, the label part) via callback. It would work if I call the widget by name (that is, $le->configure(-labelBackground => 'red'); would do the job in the code below). I would like to stick with callback however since I have several such widgets in the real application.
Please give me an advice. Thank you!
#!/perl
use strict;
use warnings FATAL => qw(all);
use Tk;
use Tk::LabEntry;
use List::Util qw(first);
use Scalar::Util qw(looks_like_number);
my $mw = MainWindow->new();
$mw->title("Test");
my $test = 8;
my $width = 250; my $length = 125;
$mw->minsize($width, $length);
my $FONT = $mw->fontCreate(-family => 'verdana',
-size => 14, -weight => 'normal');
my $le = $mw->LabEntry(-label => 'Value',
-labelPack => [qw/-side left -anchor w/],
-labelFont => '9x15bold',
-font => $FONT,
-relief => 'ridge',
-textvariable => \$test,
-width => 2,
)->pack();
$le->bind('<Key>' => sub { labelCheck($_[0]);});
MainLoop;
sub labelCheck
{
my $x = $_[0]->get();
if ( !(looks_like_number($x)) or ($x < 0) )
{
$_[0]->delete(0, 'end');
$_[0]->configure( -background => 'red');
# $_[0]->configure( -labelBackground => 'red');
}
else
{
$_[0]->configure( -background => '#f0f0f0',);
# $_[0]->configure( -labelBackground => '#f0f0f0');
do_something();
}
return 1;
}
sub do_something { print $test, $/; }