ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use Tk; my $mw = MainWindow->new; $mw->geometry("500x200"); $mw->title("Hello User"); $mw->Label(-text => 'Hello user, please Load the required fileand then + press ok')->pack(); $mw->Button(-text => "OK", -command =>sub{exit})->pack(); MainLoop;

I've written this code to generate a dialog box for a user to instruct him to load the required file manually. Now I want to save the file as well(through script not manual) but as soon as user will press "OK" in the dialog box my script ends because of the "exit" used in perl/Tk. I am new to this module so I don't know how to use it effectively. monks help me out in this please?

  • Comment on How to create a dialog box with perl/Tk that does not end your script?
  • Download Code

Replies are listed 'Best First'.
Re: How to create a dialog box with perl/Tk that does not end your script?
by tybalt89 (Monsignor) on Oct 07, 2016 at 07:03 UTC
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->geometry("500x200"); $mw->title("Hello User"); $mw->Label(-text => 'Hello user, please Load the required fileand then + press ok')->pack(); $mw->Button(-text => "OK", -command =>sub{$mw->destroy})->pack(); MainLoop; print "continue program here after exiting dialog box\n";

      Thanks Buddy. worked like anything.

Re: How to create a dialog box with perl/Tk that does not end your script?
by kcott (Archbishop) on Oct 07, 2016 at 08:41 UTC

    G'day ankit.tayal560,

    "How to create a dialog box with perl/Tk ..."

    See the Tk Distribution Page for a variety of core widgets for dialogue boxes, message popups, colour pickers, file selectors, and so on. I have this page bookmarked and refer to it often; I suggest you do the same.

    I further recommend that you generally familiarise yourself with the widgets that are available. This will help you search this page for the wanted functionality. For instance, searching for widgets matching:

    dialog
    10 matches. As a general rule, use these for multiple responses: OK/Cancel; OK/Retry/Abort; and so on.
    message
    3 matches. Use these for single response acknowledgements: "Info: Process Complete." → [OK]; "Error: Bug in code." → [Bugger!]; and so on.
    file
    color
    Multiple matches. Usage: self-explanatory.

    You can also find usage examples in the widget demo (widget & from the command line on *nix). Be aware that most of this code is very old and some of it is now discouraged in favour of newer methods. There's probably more, but two immediately came to mind.

    From the vars pragma DESCRIPTION:
    "NOTE: For use with variables in the current package for a single scope, the functionality provided by this pragma has been superseded by our declarations, available in Perl v5.6.0 or later, and use of this pragma is discouraged. See our."
    From perlobj: Indirect Object Syntax:
    "Outside of the file handle case, use of this syntax is discouraged as it can confuse the Perl interpreter. See below for more details." [original text emboldened]

    — Ken

Re: How to create a dialog box with perl/Tk that does not end your script?
by Anonymous Monk on Oct 07, 2016 at 04:45 UTC
    Um, exit is exit, tk comes with program called widget, it is examples, also tk save