yedukondalu has asked for the wisdom of the Perl Monks concerning the following question:
Given above is my code.In that I am validating the name entry not to contain any numeric values.If it does the messageBox displays and goes to the next entry..I need the cursor to be in the same entry if the validation fails. And the other is when I give any character in age enter both messageBoxes i.e dob and ages displays are popping up.
#!usr/bin/perl use warnings; use strict; use Tk; #$\="\n"; my $name; my $age; my $dob; open(my $fh,'+<','details.txt') || die "Can't open the file details.tx +t $!"; #creating a Mainwindow: my $window = MainWindow->new(); my $first_frame = $window -> Frame()->pack(-side => 'top',-ipadx => 10 +,-fill => 'x', -ipady => 1); $first_frame -> Label(-text => 'Fill the details listed below') -> gr +id(-row => 0 ,-column => 2,-rowspan => 1); my $frame = $window -> Frame(-background => 'grey')->pack(-side => 'to +p',-ipadx =>150,-fill => 'x',-ipady => 60); my $name_label=$frame -> Label(-text => 'Name') -> grid(-row => 2, -co +lumn=> 0); my $name_entry=$frame->Entry(-background => 'white',-foreground => 'bl +ack', -textvariable => \$name, -validate => 'focusout',-validatecomma +nd => \&check_name)->grid(-row =>2, -column=>1); my $age_label=$frame -> Label(-text => 'age')-> grid(-row => 4, -colum +n=> 0); my $age_entry = $frame -> Entry(-background => 'white',-foreground => +'black', -textvariable => \$age,-validate => 'focusout',-validatecomm +and => \&check_age) -> grid(-row =>4, -column=>1); my $dob_label=$frame -> Label(-text => 'DOB')-> grid(-row => 6, -colum +n=> 0); my $dob_entry = $frame -> Entry(-background => 'white',-foreground => +'black',-textvariable => \$dob, -validate => 'focusout',-validatecomm +and => \&check_dob) -> grid(-row =>6, -column=>1); $name=$name_entry -> get(); $age = $age_entry -> get(); $dob = $dob_entry -> get(); my $submit = $frame -> Button(-text => 'submit', -command =>sub {&subm +it} ) -> grid( -row =>8, -column=>1); $frame -> Label(-background => 'grey') -> grid(-rowspan=> 6); $frame -> Label(-text => 'After adding all the details click quit to e +xit') -> grid( -row => 16, -column=> 1); $frame -> Label(-background => 'grey') -> grid(-rowspan=> 6); my $quit = $frame -> Button(-text => 'Quit', -command =>sub {exit} ) - +> grid(-row =>25, -column=>1); sub submit { my $line= join(' ',$name,$age,$dob); print $fh $line,"\n"; if(!($?)) { my $button = $frame -> messageBox( -icon => 'info',-message => ' +Details added successfully to file', -type => 'Ok' ); $name_entry->delete('0', 'end'); $age_entry->delete('0', 'end'); $dob_entry->delete('0', 'end'); } } sub check_name { if (($name =~ m/[0-9]/)) { $name_entry -> messageBox( -icon => 'info',-message => 'Name shoul +d not contain numeric values' ,-type => 'Ok'); $name_entry->delete('0', 'end'); } } sub check_age { if (($age=~ m/[a-z]/i)) { $age_entry -> messageBox( -icon => 'error',-message => 'age shoul +d not contain characters' ,-type => 'Ok'); $age_entry->delete('0','end'); } } sub check_dob { if (!($dob=~ m/\d{1,2}[\/|\:]\d{1,2}[\/|\:]\d{4}/)) { $age_entry -> messageBox( -icon => 'error',-message => 'Enter vali +d date' ,-type => 'Ok'); $dob_entry->delete('0', 'end'); } } MainLoop;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how can i be in the same entry if my validation fails in Tk? (blinking cursor is Tk::focus)
by Anonymous Monk on Nov 25, 2015 at 08:42 UTC | |
by yedukondalu (Acolyte) on Nov 25, 2015 at 09:03 UTC | |
by Anonymous Monk on Nov 25, 2015 at 09:13 UTC |