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

hi, i have written a simple Perl script using Tk. it just displays a window with some message. i want that the window should go away automatically after 2 seconds, and the script should exit. returning focus to the konsole window. any ideas how to do it? Regards, Shobhit.
  • Comment on autoclose a Perl-Tk window after 2 seconds.

Replies are listed 'Best First'.
Re: autoclose a Perl-Tk window after 2 seconds.
by shmem (Chancellor) on Apr 09, 2007 at 11:52 UTC
    As said in the CB,
    use Tk; my $mw = MainWindow->new; $mw->Label(-text => "Foo")->pack; $mw->after(2000, sub {exit}); MainLoop;

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      shmem's answer is exactly correct.

      However, because I've encountered this at various times, I'd like to share another point that sooner or later might be useful to whomever.

      When you simply want the MainLoop to finish, and continue executing code after it, you don't want to exit, but instead kill the MainWindow:

      $mw->after(2000, sub { $mw->destroy }); MainLoop; warn "MainLoop ended; continuing...\n";
      A word spoken in Mind will reach its own level, in the objective world, by its own weight
        Thanks everyone for their collective wisdom in solving my problem.