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

In $poe_main_window I create a $button. Also in that button I have a key binding on the key c that does $button->invoke. That button , opens a new window with $poe_main_window->TopLevel; and in that window there is a text widget. Only problem is,that when one writes stuff in that window and "accidentally" types the key c it triggers the binding that was binded for $poe_main_window altough that binding was mean for the $poe_main_window *ONLY*.

$poe_main_window->bind('all','<c>', sub{ $button->invoke; } )

I'm currently reading some book on Tk to see if I can fix the problem.I'd be interested in your thoughts also on solving the problem.

Replies are listed 'Best First'.
Re: a binding handles keys pressed in unwanted windows
by liverpole (Monsignor) on Sep 21, 2007 at 14:25 UTC
    Hi spx2,

    It's quite easy to do ... just use the name of the main window in the call to bind instead of the word 'all'.  For example:

    #!/usr/bin/perl -w use strict; use warnings; use Tk; # Main program my $poe_main_window = new MainWindow(-title => 'Tk Binding example'); my $bg = "green"; my $psub = sub { new_toplevel($poe_main_window) }; my $text = "New Window ('c')"; my @args = (-bg => $bg, -command => $psub, -text => $text); my $button = $poe_main_window->Button(@args)->pack(); $poe_main_window->bind($poe_main_window, '<c>', sub{ $button->invoke } + ); MainLoop; # Subroutines sub new_toplevel { my ($mw) = @_; my $top = $mw->Toplevel(-title => "New TopLevel"); my $lbl = $top->Label(-bg => 'skyblue', -text => "Text Widget")->p +ack(); my $txt = $top->Text(-height => 16, -width => 120)->pack(); }

    It's the line:

    $poe_main_window->bind($poe_main_window, '<c>', sub{ $button->invoke } + );

    which binds the 'c' character only to the window $poe_main_window, rather than any other Toplevel widgets created from it.

    Update:  You can get more information on binding with:  perldoc Tk::bind.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/