#!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new(-title => 'Mac Address Input Example'); my $fr = $mw->Frame()->pack(-expand => 1, -fill => 'both'); my $lb = $fr->Label(-text => 'MAC Address')->pack(-side => 'left'); my %entries; for(1..6){ $entries{$_}{'entry'} = $fr->Entry(); $entries{$_}{'entry'}->configure( -textvariable => \$entries{$_}{'addy'}, -width => 3, -validate => 'focusout', -validatecommand => [ \&validate,$_ ], -invalidcommand => [ \&show_invalid,$entries{$_}{'entry'} ], ); $entries{$_}{'entry'}->pack(-side => 'left'); } MainLoop(); # returns `1' if valid, and `0' if invalid sub validate { my($num,$val) = @_; unless($val){ print "Field $num has nothing to validate\n";return 1;} my $valid = ($val =~ /^[0-9a-f]{1,2}$/i) ? 1 : 0; printf "Field %s, validating \`%s'...%s\n",$num,$val,$valid? "ok": "FAILED"; return $valid; } sub show_invalid { my($widget) = @_; $widget->focus(); # temporarily flash the background of the problem field in red my $bg = $widget->cget('-bg'); $widget->configure(-bg => 'red'); $widget->update(); # effect a sleep of 200 milliseconds select(undef,undef,undef,0.2); # return the background color to the original state $widget->configure(-bg => $bg); $widget->update(); } #### for(1..6){ $entries{$_}{'addy'} = 'zz'.$_ if($_ == 3);