Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Enter Key focus is not happening

by gocpon (Novice)
on May 07, 2012 at 07:39 UTC ( #969208=perlquestion: print w/ replies, xml ) Need Help??
gocpon has asked for the wisdom of the Perl Monks concerning the following question:

Dear All, I have created Labels and Entry widget in loop ,but when i tried to implement enter key event like if press entry key then focus should go to next entry widget not other widget,Please correct my code. ------------
#!/usr/bin/perl use Tk; use Tk::Entry; use Tk::Table; use Tk::LabFrame; use warnings; use strict; my $v="0.00"; my $mw = MainWindow->new; $mw->geometry("200x200"); $mw->resizable(0,0); $mw->title("Table Example"); my @id=qw/100 101 102 103 104 105 106 107 108 109 110/; my @name=qw/AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK/; my @top2=qw/No Name Amount/; my $table_frame = $mw->LabFrame()->pack(); my $table = $table_frame->Table(-columns => 10, -rows => 6, -fixedrows + => 1, -scrollbars => 'oe', -relief => 'raised',-background=>'white'); my $j=0; my $i=0; foreach my $j(1..11) { my $tmp_label=$table->Label(-text => $id[$i], -width => 8, -relief =>' +raised'); my $tmp_label1 = $table->Label(-text => $name[$i], -width => 8, -relie +f =>'raised'); my $tmp_label2= $table->Entry( -width => 8, -relief =>'raised',-valida +te=>'key',-validatecommand=>sub{$_[1]=~m/[\d\d]/} ,-invalidcommand=>sub{$mw->bell} ); $tmp_label2->bind('Tk::Entry','<Return>',sub{shift->focusNext}); $table->put($j,1,$tmp_label); $table->put($j,2,$tmp_label1); $table->put($j,3,$tmp_label2); $i++; } $table->pack(); my $button_frame = $mw->Frame( -borderwidth => 4 )->pack(); $button_frame->Button(-text => "Exit", -command => sub {exit})->pack() +; #$button_frame->Button(-t)->pack(); MainLoop;

Comment on Enter Key focus is not happening
Download Code
Re: Enter Key focus is not happening
by zentara (Archbishop) on May 07, 2012 at 11:00 UTC
    You should wrap your code in a code block, so we don't have to reconstruct your code. See Writeup Formatting Tips

    Your code, as you wrote above in html, is missing all the brackets needed to specify array elements. The code I believe you meant to post, as reconstructed by me, is shown below. I had to remove your resizable and geometry lines to get the Entries to show up, and I made the bg color white on the Entries for ease of visibility.

    As far as getting focus to shift the way you want, I'm stumped, maybe a smarter monk may know. It may be in the way Table is designed. An alternative would be to use TableMatrix, see the second code example below.

    Your corrected, runnable code:

    #!/usr/bin/perl use Tk; use Tk::Entry; use Tk::Table; use Tk::LabFrame; use warnings; use strict; my $v="0.00"; my $mw = MainWindow->new; #$mw->geometry("200x200"); #$mw->resizable(0,0); $mw->title("Table Example"); my @id=qw/100 101 102 103 104 105 106 107 108 109 110/; my @name=qw/AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK/; my @top2=qw/No Name Amount/; my $table_frame = $mw->LabFrame()->pack(); my $table = $table_frame->Table(-columns => 10, -rows => 6, -fixedrows => 1, -scrollbars => 'oe', -relief => 'raised',-background=>'white'); my $j=0; my $i=0; foreach my $j(1..11) { my $tmp_label=$table->Label(-text => $id[$i], -width => 8, -relief =>'raised'); my $tmp_label1 = $table->Label(-text => $name[$i], -width => 8, -relief =>'raised'); my $tmp_label2= $table->Entry( -width => 8, -relief =>'raised', -bg => 'white', -validate=>'key',-validatecommand=>sub{ $_[1] =~m/\d\d/} , -invalidcommand=>sub{$mw->bell} ); $tmp_label2->bind('Tk::Entry','<Return>',sub{shift->focusNext}); $table->put($j,1,$tmp_label); $table->put($j,2,$tmp_label1); $table->put($j,3,$tmp_label2); $i++; } $table->pack(); my $button_frame = $mw->Frame( -borderwidth => 4 )->pack(); $button_frame->Button(-text => "Exit", -command => sub {exit})->pack() +; MainLoop;

    An alternative using TableMatrix, in which focus is passed the way you want by tabbing:

    #!/usr/bin/perl use Tk; use Tk::TableMatrix; use warnings; use strict; my $mw = MainWindow->new; my $table = $mw->Scrolled("TableMatrix", -resizeborders=>'both', -titlerows => 1, -rows => 20, -colstretchmode=>'all', -cols => 3, -cache => 1, -scrollbars => "osoe"); for(my $row = 1; $row < 20; $row++) { my $entry = $table->Entry(-bg=>'white'); $table->windowConfigure("$row,5", -window => $entry); } $table->set("0,0", "Col 1"); $table->set("0,1", "Col 2"); $table->set("0,2", "Col 3"); $table->set("0,3", "Col 4"); $table->set("0,4", "Col 5"); $table->pack(-expand => 1, -fill => 'both'); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Enter Key focus is not happening
by zentara (Archbishop) on May 07, 2012 at 14:00 UTC
    Hey again, I thought about this and tried to force the focus order to the entries, and had some partial success which MAY help you. The method I used somehow interferes with the -validate command, also if the next entry is not within view of the scrolled region, the focus advance stops. Anyways, here it is, maybe you can get some mileage out of it. Good luck.
    #!/usr/bin/perl use Tk; use Tk::Entry; use Tk::Table; use Tk::LabFrame; use warnings; use strict; my $v="0.00"; my $mw = MainWindow->new; $mw->geometry("250x400"); #$mw->resizable(0,0); $mw->title("Table Example"); my @id=qw/100 101 102 103 104 105 106 107 108 109 110/; my @name=qw/AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK/; my @top2=qw/No Name Amount/; my $table_frame = $mw->LabFrame()->pack(-expand=>1, -fill=>'both'); my $table = $table_frame->Table(-columns => 10, -rows => 6, -fixedrows => 1, -scrollbars => 'oe', -relief => 'raised',-background=>'white'); my $j=0; my $i=0; my @ents; foreach my $j(1..11) { my $tmp_label=$table->Label(-text => $id[$i], -width => 8, -relief =>'raised'); my $tmp_label1 = $table->Label(-text => $name[$i], -width => 8, -relief =>'raised'); my $tmp_label2= $table->Entry( -width => 8, -relief =>'raised', -bg => 'white', -validate=>'key', #-validatecommand=>sub{ $_[1] =~m/\d\d/} , #-invalidcommand=>sub{$mw->bell} ); #$tmp_label2->bind('Tk::Entry','<Return>',sub{ shift->focusNext }); $table->put($j,1,$tmp_label); $table->put($j,2,$tmp_label1); $table->put($j,3,$tmp_label2); push @ents, $tmp_label2; $i++; } $table->pack( -expand=>1 , -fill=>'both'); my $button_frame = $mw->Frame( -borderwidth => 4 )->pack(); $button_frame->Button(-text => "Exit", -command => sub {exit})->pack() +; &defineOrder( @ents ); MainLoop; sub defineOrder { my $widget; for (my $i=0; defined( $_[$i+1] ); $i++) { $_[$i]->bind( '<Key-Return>', [\&focus, $_[$i+1]]); $_[$i]->bind( '<Tab>', [\&focus, $_[$i+1]]); } # Uncomment this line if you want to wrap around $_[ $#_ ]->bind('<Key-Return>', [\&focus, $_[0]]); $_[ $#_ ]->bind('<Tab>', [\&focus, $_[0]]); $_[0]->focus; } sub focus { my ($tk, $self) = @_; $self->focus; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thank you very much..

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://969208]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2014-12-19 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (70 votes), past polls