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

Dear Monks, I'm a perl newbie and right now I'm trying to create a small utility to view a bunch of text files and everytime I hit the ESC key all my individual text windows close. I only want the window with focus to close. What am I doing wrong? Thanks for any advice on this.
#!/usr/bin/perl -w use Tk; use strict; foreach (@ARGV) { my $x = MainWindow->new; $x->title("$_"); $x->Label(-text => "$_")->pack(-fill => 'both'); $x->bind("<Key-Escape>", sub { exit }); my $y = $x->Scrolled("Text", -height => 10, -scrollbars => + 'osoe')->pack(); open (F, "< $_") || die "Can't open"; while(<F>) { $y->insert('end', $_); } close(F); } MainLoop;

Replies are listed 'Best First'.
Re: Tk bind question
by integral (Hermit) on Feb 22, 2003 at 07:48 UTC
    The problem is caused by the fact that you are binding <Key-Escape> to sub { exit }. If you lookup exit in perlfunc, you find it performs the syscall to shutdown your whole program. What you need instead is to have a closure on $x which call's the relevant function to destroy the MainWindow.
    for (@ARGV) { my $x = MainWindow->new; $x->bind("<Key-Escape>", sub { $x->destroy }); }

    --
    integral, resident of freenode's #perl