Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Perl/Tk and exit(0)

by saw55 (Novice)
on Apr 02, 2020 at 23:49 UTC ( [id://11114967]=note: print w/replies, xml ) Need Help??


in reply to Re: Perl/Tk and exit(0)
in thread Perl/Tk and exit(0)

Thanks for the reply. I know exit causes the program to exit, but I believe the call to the subroutine preceding the exit statement should cause the message window to display before the program exits. I know sub timedDialog executes because I inserted a print statement in it and it does indeed print but the message window never displays; and I put a sleep statement between the sub call and exit in hope of allowing time for the message to display but no go.

I just noticed I called timedDialog both in sub checkDays and in sub setupGUI I fixed this and still no message shows

Fixed code follows

#!/usr/bin/perl ###################################################################### +## use strict; use warnings; use Tk; my $mw = MainWindow -> new; my $timedDialogTitle = ''; my $timedDialogText = ''; my $svBtn = undef; #Option window SAVE button. &setupGUI; $mw->deiconify(); $mw->raise(); MainLoop; exit(0); ################################################ ################################################ sub setupGUI{ $svBtn = $mw->Button( -text => "SAVE", -command => sub {&checkDays +; exit(0);}); $svBtn->grid(-row => 9, -column => 2, -sticky => 'e'); $mw->bind('<KeyPress-Return>' => sub {&checkDays; exit(0);}); $mw-> withdraw(); } ##################################### sub checkDays { &timedDialog("Exiting", "O.K., no backup will be made, + then....Exiting", 25_000); sleep 15; } ##################################### sub timedDialog { print ("in timedDialog\n"); my $subwindow = MainWindow->new; $subwindow->geometry("490x150+400+400"); $subwindow->title($_[0]); my $label = $subwindow->Label(-text => $_[1]); $label->pack; $subwindow->after($_[2], sub {$subwindow->destroy;}); } #####################################

Replies are listed 'Best First'.
Re^3: Perl/Tk and exit(0)
by jcb (Parson) on Apr 03, 2020 at 01:56 UTC

    All UI activity in Tk occurs in the Tk event loop. You are exiting the program from an event handler before returning control to the event loop. Try replacing that handler with sub {&checkDays; $mw->after(5000, sub {exit(0)});} and see Tk::after for more details.

      Thanks for the reply, but I could use some clarification if you could. Are you saying I should replace

      $svBtn = $mw->Button( -text => "SAVE", -command => sub {&checkDays; exit(0);});

      with

      $svBtn = $mw->Button( -text => "SAVE", -command => sub {&checkDays; $mw->after(5000, sub {exit(0)});}

      When I do that I get an error saying "Scalar found where operator expected" at the next line, near "$mw" I guess I am sort of lost.

        missing ); on replacement statement;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found