My output:
page2 raised
Going to fire page2 entryKeyRelease
page1 entry KeyRelease fired
Any thing happened ?
page2 entry KeyRelease fired
####
use strict;
use warnings;
use Tk;
use Tk::NoteBook;
# Main Entry
my $top = MainWindow->new(); $top->title("Test");
my $nb = $top->NoteBook()->pack(-expand => 1, -fill => 'both');
InitializeNoteBookPage(\$nb, \$nb->add('page1', -label => 'Page 1'), "Label for Page 1");
InitializeNoteBookPage(\$nb, \$nb->add('page2', -label => 'Page 2'), "Label for Page 2");
Tk::MainLoop();
# Subroutines start here
sub InitializeNoteBookPage{
my($nb, $nbpage, $label_description) = @_;
# Widget Initialization
my($_label_1) = $$nbpage->Label( -text => $label_description, );
my($_entry_1) = $$nbpage->Entry( -width => 0, );
# Geometry Management
$_label_1->grid( -in => $$nbpage, -column => 1, -row => 1, -columnspan => 1, -ipadx => 0, -ipady => 0, -padx => 0, -pady => 0, -rowspan => 1, -sticky => "" );
$_entry_1->grid( -in => $$nbpage, -column => 2, -row => 1, -columnspan => 1, -ipadx => 0, -ipady => 0, -padx => 0, -pady => 0, -rowspan => 1, -sticky => "ew" );
# Resize Behavior
$$nbpage->gridRowconfigure (1, -weight => 0, -minsize => 50, -pad => 0);
$$nbpage->gridColumnconfigure(1, -weight => 0, -minsize => 400, -pad => 0);
$$nbpage->gridColumnconfigure(2, -weight => 0, -minsize => 400, -pad => 0);
# Event callbacks
$_entry_1->bind("", sub {
print $$nbpage->name()." ".$_entry_1->name()." KeyRelease fired\n";
} );
$$nb->pageconfigure($$nbpage->name(), -createcmd => sub {
print "\n".$$nbpage->name()." created \n";
} );
$$nb->pageconfigure($$nbpage->name(), -raisecmd => sub {
print "\n".$$nbpage->name()." raised \n";
print "Going to fire ".$$nbpage->name()." ".$_entry_1->name()."KeyRelease\n";
$_entry_1->focus(); # If this line is commented out, no event will be fired at all ?!
$_entry_1->eventGenerate("");
print "Any thing happened ?\n";
} );
}
####
D:\Test>perl -v
This is perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x86-multi-thread-64int
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2015, Larry Wall
Binary build 2200 [299195] provided by ActiveState http://www.ActiveState.com
Built Jul 20 2015 18:55:28
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
D:\Test>perl test2.pl
page1 created
page1 raised
Going to fire page1 entryKeyRelease
Any thing happened ?
page2 created
page2 raised
Going to fire page2 entryKeyRelease
Any thing happened ?
page1 raised
Going to fire page1 entryKeyRelease
page2 entry KeyRelease fired
Any thing happened ?
page2 raised
Going to fire page2 entryKeyRelease
page1 entry KeyRelease fired
Any thing happened ?
page1 raised
Going to fire page1 entryKeyRelease
page2 entry KeyRelease fired
Any thing happened ?
####
I believe the reason why the bindings aren't set, when the pages are first displayed, is because the pages were originally created without those bindings. You subsequently configure the pages ($nb->pageconfigure) and, when the pages are subsequently raised, those bindings are available.
Compare -createcmd and -raisecmd in Tk::NoteBook.