Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Perl/Tk and exit(0)

by kcott (Archbishop)
on Apr 03, 2020 at 08:03 UTC ( [id://11114978]=note: print w/replies, xml ) Need Help??


in reply to Perl/Tk and exit(0)

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.

— Ken

Replies are listed 'Best First'.
Re^2: Perl/Tk and exit(0)
by saw55 (Novice) on Apr 03, 2020 at 10:59 UTC

    Thanks you so much, and thanks to jcb as well. As I am sure you you can tell, I am new to Tk, but this gives me something to work with. I knew there were standard popups available--I use them elsewhere in the program--but I think I couldn't figure out how to close them programatically.

      "Thanks you so much"

      You are very welcome.

      "I knew there were standard popups ... but I think I couldn't figure out how to close them programatically."

      Take a look at the invoke() method of Tk::Button. You could give the option to close manually via a button or, if that option isn't taken, close programmatically after the specified delay.

      — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114978]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-26 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found