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

The code below initializes a two-page Tk::NoteBook, each page of which contains a Tk::Entry. It tries to generate <KeyRelease> event for Tk::Entry when its parent Tk::NoteBook page is raised, but fails.

Specifically, it seems strange that :

1. the Tk::Entry's KeyRelease event handler will **not** be called at the first **two** page raise event triggered by mouse click;

2. starting from the third page raise event triggered by mouse click, **wrong** Tk::Entry's KeyRelease event handler will be called;

3. if Tk::Entry->focus is commented out in the page raise event handler, no <KeyRelease> event will be fired for Tk::Entry at all.

Could you comment about the reason of the error and suggest how to correctly trigger the Tk::Entry KeyRelease event in its container page's raise event ?

PS: Tk 804.033 & ActiveState Perl 5.20.2.2002 & Windows 10 10240 X64

The code:

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->add('page1', -label => 'Page 1'), "Lab +el for Page 1"); InitializeNoteBookPage($nb->add('page2', -label => 'Page 2'), "Lab +el for Page 2"); Tk::MainLoop(); # Subroutines start here sub InitializeNoteBookPage{ my($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, -col +umnspan => 1, -ipadx => 0, -ipady => 0, -padx => 0, -pady => 0, -rows +pan => 1, -sticky => "" ); $_entry_1->grid( -in => $nbpage, -column => 2, -row => 1, -col +umnspan => 1, -ipadx => 0, -ipady => 0, -padx => 0, -pady => 0, -rows +pan => 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("<KeyRelease>", sub { print $nbpage->name()." ".$_entry_1->name()." KeyRelease f +ired\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 <K +eyRelease> event will be fired at all ?! $_entry_1->eventGenerate("<KeyRelease>"); print "Any thing happened ?\n"; } ); }

The output:

page1 raised Going to fire page1 entryKeyRelease Any thing happened ? 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 <--- using "Tab" + "LeftRight" + + "Space" Going to fire page1 entryKeyRelease Any thing happened ? page1 entry KeyRelease fired

Replies are listed 'Best First'.
Re: Fail to generate event for Tk::Entry contained in Tk::NoteBook page
by kcott (Archbishop) on Dec 14, 2015 at 19:39 UTC

    G'day xcli,

    Welcome to the Monastery.

    And ++ for a most commendable first post: if only we could get more (first posts) like that.

    I tested your code. Beyond adding a #!/usr/bin/env perl shebang line, your posted source code was unchanged.

    My system: Perl 5.22.0; Tk 804.033; Mac OS X 10.10.3

    I do recall that I had major problems with Tk and Perl 5.20 versions. My perlbrew list indicates I have both 5.20.0 and 5.20.2. I don't recall details except that I pretty much gave up on any Tk and Perl 5.20 combination. This could potentially be related to issues you're encountering (but that's very much a guess). Normally, I would just switch to 5.20.2 and test with the same Perl version as you; unfortunately, I'm unable to do that in this instance.

    On to my testing:

    Using the mouse to change pages, the first four blocks of "output" you show were the same for me (including the blank lines). Then, as soon as I hit Tab (to start your "Tab" + "LeftRight" + "Space" sequence), I got "page2 entry KeyRelease fired" as a continuation of the the fourth block.

    Your posted output:

    page2 raised Going to fire page2 entryKeyRelease page1 entry KeyRelease fired Any thing happened ? page1 raised

    My output:

    page2 raised Going to fire page2 entryKeyRelease page1 entry KeyRelease fired Any thing happened ? page2 entry KeyRelease fired

    [Aside: I notice that you've indented everything you've posted within <code>...</code> tags. It's good that you've used those tags; however, their main purpose is to allow us to see exactly what you're seeing (i.e. a verbatim copy). While not a major issue here, it could be elsewhere, so please copy and paste code and data without any modifications. Thanks.]

    In general, your bindings seem to be working fine. Just running the GUI and typing 123 (without any other mouse or key activity) results in:

    page1 raised Going to fire page1 entryKeyRelease Any thing happened ? page1 entry KeyRelease fired page1 entry KeyRelease fired page1 entry KeyRelease fired

    Similarly, starting the GUI afresh and just holding down the spacebar gives:

    page1 raised Going to fire page1 entryKeyRelease Any thing happened ? page1 entry KeyRelease fired page1 entry KeyRelease fired page1 entry KeyRelease fired page1 entry KeyRelease fired page1 entry KeyRelease fired page1 entry KeyRelease fired

    and those lines just repeated until I stopped holding down the spacebar.

    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. You'll basically need to look at the order in which you're doing things. Compare -createcmd and -raisecmd in Tk::NoteBook.

    I'd also be wary of creating closures over global variables in your callbacks. I posted information about this two days ago: "Re: Returning a value from a Perl Listbox".

    See also Tk::event and Tk::bind.

    — Ken

      Hello Ken,

      Thank you very much for your time and kind help !

      My output: page2 raised Going to fire page2 entryKeyRelease page1 entry KeyRelease fired Any thing happened ? page2 entry KeyRelease fired

      In your output, even though there is a strange "page1 entry KeyRelease fired", "page2 entry KeyRelease fired" matches "Going to fire page2 entryKeyRelease". However, I still could not fire the correct KeyRelease event, after uninstalling ActivePerl 5.20.2.2002 and installing ActivePerl 5.22.0.2200. Specifically, when page1 is raised, the KeyPress event is unexpectedly fired for the entry in page2.

      The code modified: (1) add -createcmd (2) change argument types

      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, -column +span => 1, -ipadx => 0, -ipady => 0, -padx => 0, -pady => 0, -rowspan + => 1, -sticky => "" ); $_entry_1->grid( -in => $$nbpage, -column => 2, -row => 1, -column +span => 1, -ipadx => 0, -ipady => 0, -padx => 0, -pady => 0, -rowspan + => 1, -sticky => "ew" ); # Resize Behavior $$nbpage->gridRowconfigure (1, -weight => 0, -minsize => 50, -p +ad => 0); $$nbpage->gridColumnconfigure(1, -weight => 0, -minsize => 400, -p +ad => 0); $$nbpage->gridColumnconfigure(2, -weight => 0, -minsize => 400, -p +ad => 0); # Event callbacks $_entry_1->bind("<KeyRelease>", sub { print $$nbpage->name()." ".$_entry_1->name()." KeyRelease fire +d\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 <KeyRe +lease> event will be fired at all ?! $_entry_1->eventGenerate("<KeyRelease>"); print "Any thing happened ?\n"; } ); }

      The output

      D:\Test>perl -v This is perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x +86-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.ActiveSt +ate.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 ki +t. 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 Pa +ge. 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 ?

      Your helpful comments below makes sense.

      I believe the reason why the bindings aren't set, when the pages are f +irst displayed, is because the pages were originally created without +those bindings. You subsequently configure the pages ($nb->pageconfig +ure) and, when the pages are subsequently raised, those bindings are +available. Compare -createcmd and -raisecmd in Tk::NoteBook.