Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

TK button on pressing enter

by howarda (Acolyte)
on Aug 30, 2005 at 11:58 UTC ( [id://487716]=perlquestion: print w/replies, xml ) Need Help??

howarda has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am creating a simple logon window with two Labels for the user name and password and two buttons for Login and Exit.

What I have been trying to do is to get the Login button to be pressed upon the user hitting the enter key after the password is typed in rather than them having to use the mouse to actually press the button.

The script itself works so far apart from this minor piece of formatting

Forgive this rather mundane question but I can't find the answer in the documentation.

Many thanks
my $main = MainWindow->new; $main->configure(-title => 'Verify User', -background=>'blue'); $main->geometry('+300+300'); my $top = $main->Frame(-background=>'blue', -relief=>'sunken')->pack(-side=>'top',-fill=>'x'); my $left1 = $top->Frame(-background=>'blue', )->pack(-side=>'left',pady=>9,padx=>8); my $left2 = $top->Frame(-background=>'blue', )->pack(-side=>'left',pady=>9,padx=>8); my $user_frm = $left1->Frame(-relief => 'groove', -borderwidth=>3, -background=>'blue', )->pack(-side=>'top',-fill=>'x'); my $user_lbl = $user_frm->Label(-text=> 'Enter Username', -background=>'blue',-foreground=>'white', )->pack(-side=>'left'); my $user_en = $user_frm->Entry(-width=>8, -background=>'white',)->pack(-side=>'left',-pad +y=>3); my $pass_frm = $left1->Frame(-relief => 'groove', -borderwidth=>3, -background=>'blue', )->pack(-side=>'top',-fill=>'x'); my $pass_lbl = $pass_frm->Label(-text=> 'Enter Password', -background=>'blue',-foreground=>'white', )->pack(-side=>'left'); my $pass_en = $pass_frm->Entry(-width=>12, -show=>'*', -background=>'white',)->pack(-side=>'left',-pad +y=>3); my $button = $left2 ->Button(-text => 'Exit', -underline => 0, -width => 10, -borderwidth=>3, -command => sub{do_exit()} )->pack; my $button1 = $left2 ->Button(-text => 'Connect ', -width => 10, -borderwidth=>3, -command => sub{do_submit("$user_en, $pass_en")} )->pack; MainLoop;

Replies are listed 'Best First'.
Re: TK button on pressing enter
by liverpole (Monsignor) on Aug 30, 2005 at 12:34 UTC
    I think the function you're looking for is "invoke", which causes the widget object (in this case, the Login button) to be pressed when 'Return' is entered.

    Since you only want this occur when the Password entry widget is active, you should bind the '<Return>' key to $pass_en (and note that you have to do it -after- button1 has been defined).  Try the following (noting that the line in blue is the crucial one):

    
    use Tk;
    my $main = MainWindow->new;
    $main->configure(-title => 'Verify User', -background=>'blue');
    $main->geometry('+300+300');
    my $top = $main->Frame(-background=>'blue',
        -relief=>'sunken')->pack(-side=>'top',-fill=>'x');
    my $left1 = $top->Frame(-background=>'blue',
        )->pack(-side=>'left',-pady=>9,-padx=>8);
    my $left2 = $top->Frame(-background=>'blue',
        )->pack(-side=>'left',-pady=>9,-padx=>8);
    my $user_frm = $left1->Frame(-relief => 'groove',
                        -borderwidth=>3, -background=>'blue',
                        )->pack(-side=>'top',-fill=>'x');
    my $user_lbl = $user_frm->Label(-text=> 'Enter Username',
                            -background=>'blue',-foreground=>'white',
                            )->pack(-side=>'left');
    my $user_en = $user_frm->Entry(-width=>8,
                           -background=>'white',)->pack(-side=>'left',-pady=>3);
    my $pass_frm = $left1->Frame(-relief => 'groove',
                        -borderwidth=>3, -background=>'blue',
                        )->pack(-side=>'top',-fill=>'x');
    my $pass_lbl = $pass_frm->Label(-text=> 'Enter Password',
                            -background=>'blue',-foreground=>'white',
                            )->pack(-side=>'left');
    my $pass_en = $pass_frm->Entry(-width=>12,
                           -show=>'*',
                           -background=>'white',)->pack(-side=>'left',-pady=>3); 
    my $button = $left2 ->Button(-text => 'Exit', -underline => 0,
                         -width => 10,
                         -borderwidth=>3,
                         -command => sub{do_exit()}
                         )->pack;
    my $button1 = $left2 ->Button(-text => 'Connect ',
                         -width => 10,
                         -borderwidth=>3,
                         -command => sub{do_submit("$user_en, $pass_en")}
                         )->pack;
    
    $pass_en->bind("<Return>", sub { $button1->invoke });
    
    
    MainLoop;
    
    sub do_submit() { print "Called 'do_submit'\n"; }
    sub do_exit()   { print "Called 'do_exit'\n"; }
    


    Note that I've supplied two stubs so that you'll have something to actually call when the buttons are invoked.   I've also added '-' to a few switches where it was missing, to get rid of warnings (eg. "pady" => "-pady").  Hope this helps!

      liverpole is correct. But, to be complete, you also want to add a binding to $user_en

      $user_en->bind("<Return>", sub { $button1->invoke });

      Many thanks, that has worked a treat.
        ++ Very nice, I've been tinkering with Tk a little more lately and the more I romp around the more I learn. This is exactly what I needed $main->geometry('+300+300');

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-29 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found