G'day saw55,
Welcome to the Monastery.
I see the reason for your problem has been explained by ++jcb.
Instead of calling exit(0) as you currently do,
consider passing a flag that instructs &timedDialog to either
destroy the dialogue or exit the application.
Here's a fully functional, albeit extremely barebones, example of what I mean.
#!/usr/bin/env perl
use strict;
use warnings;
use Tk;
{
my $mw = MainWindow::->new();
$mw->Button(
-text => 'Transient message',
-command => sub { out_msg(\$mw, 'Message ...', 2_000) },
)->pack();
$mw->Button(
-text => 'No backup',
-command => sub { out_msg(\$mw, 'Exiting ...', 2_000, 1) },
)->pack();
}
sub out_msg {
my ($mw_ref, $msg, $delay, $exit) = @_;
my $tl = $$mw_ref->Toplevel();
$tl->Label(-textvariable => \$msg)->pack();
my $handle_msg = $exit
? sub { exit }
: sub { $tl->destroy };
$tl->after($delay, $handle_msg);
}
MainLoop;
Note that the flag is only needed for those callbacks where you want to exit:
you may only need to make minimal changes if you adopt this technique.
Here's a few other points that aren't directly related to your current problem:
-
Avoid referencing external variables in your subroutines; pass them in as arguments.
Prefer references to variables rather than the variable's value;
this will avoid unexpected bugs, that are often hard to track down, in your Tk scripts.
I've shown \$mw and $mw_ref in my code as an example;
admittedly, this was somewhat contrived but there were few opportunities for examples
in such a short script — it's still a valid example and, if you run that code,
you'll see it works without any problems.
-
You might also note that $mw does not have file scope.
It is constrained to an anonymous block and completely inaccessible by &out_msg.
-
Prefer calling subroutines without a leading ampersand; i.e. subname() in favour of &subname.
The &subname and &subname(@arg_list) forms are generally not what you want and can lead to problems.
See perlsub for details.
-
Probably more for future reference: there are many dialogue and messaging widgets
that are part of the core Tk distribution.
See the Popups and Dialogs section of the Tk documentation.