This is a discussion on Example of passing focus widget to widget automatically with <Enter> - Perl ; Well I wanted my program to smoothly pass the focus from one widget to another with <return> since this is how a LOT of programs work on different platforms (not just Windows). It took me a while to figure this ...
Well I wanted my program to smoothly pass the focus from one widget to
another with <return> since this is how a LOT of programs work on
different platforms (not just Windows).
It took me a while to figure this out, so I am posting this sample
program for others.
It passes focus to the next widget in the sequence either when the left
mouse button is double clicked or when the <Enter> key is pressed.
For double click to work properly on the scale the mouse has to be over
the slider of the scale, otherwise the value of the scale selection
changes during the double click.
In general one does not want the double click to advance but I thought
I would test it out anyway.
### BEGIN CODE ###
use Tk;
use strict;
my $item;
my $foundmessage;
my $count ='0';
my @match= qw/lily snapdragon iris cherry rose tulip sunflower aster
poppy peach daffodil dahlia pansy daylilly forget_me_not/;
my $mw = MainWindow -> new (-title =>"$0", );
my $get_entry = $mw -> Entry( '-width' =>40,
-takefocus => 1,
-borderwidth =>5,
-foreground => "\#660000",
-background => "\#ffffff",
-relief => 'flat',
-text => "entert text here",
)->pack();
$get_entry->bind("<Return>", \&do_entry);
$get_entry->bind("<Double-Button-1>", \&do_entry);
my $found_lb = $mw -> Listbox(-width =>30,
-background => "\#ffffff",
-selectmode => 'browse',
-exportselection => 0,
-setgrid => 1,
-takefocus => 1,
)->pack();
$found_lb -> insert('end', @match);
$found_lb -> bind("<Double-Button-1>",\&get_item);
$found_lb -> bind("<Return>",[\&get_item, Ev ("K")]);
my $number = $mw -> Scale(-label => "4: Select Number from Scale",
-length => 300,
-width=>12,
-orient => 'h',
-digits => 2,
-showvalue=>1,
-from => 0,
-to => 99,
-resolution => 1,
-tickinterval => 20,
-takefocus =>1,
-variable => \$count)->pack();
$number -> bind("<Return>", \&do_scale );
$number -> bind("<Double-Button-1>",\&do_scale);
my $ex_but = $mw-> Button(-text =>"Exit all",
-takefocus =>0,
-anchor =>'w',
-command =>\&exitProgram)->pack();#
MainLoop;
sub do_entry{
$found_lb -> focusForce;
my $word = $get_entry -> get();
print "you entered $word\n";
}
sub do_scale {
print "you chose $count\n";
$get_entry -> focusForce; #passes focus to next widget
}
sub get_item {
$foundmessage="";
$item ="";
my $picked = $found_lb -> curselection;
if ( $picked eq ""){
$foundmessage = "Error: You must \n pick a name \n from the
list";
}
$item .= $found_lb -> get($picked);
$foundmessage = "you picked \n $item\n ";
print "$foundmessage \n ";
$number ->focusForce; #passes focus to next widget
} #endsub
sub exitProgram{
$mw -> messageBox(-message =>"So long\! \n");
exit;
} #endsub
### END CODE ####