#!/usr/bin/perl -w
use Tk;
my $mw = MainWindow->new;
$mw->Button (-text => "Hello World!", -command => sub {exit})->pack;
MainLoop;
And this is fine. The problem is that I seek not to exit the program (the command says 'exit'), but rather to return from 'MainLoop' so I can do something else. It is possible that I could have several instances of code like this, and I'd rather not keep going thru memory with wierd returns from code (shades of 'goto'). If I make the command executed by the button just 'return', I'll just return and await the next event (another button press!). Does Perl/Tk have something that will cause 'MainLoop' to return to its caller. The documentation is a bit vague on this point!
Thanks.
Destroy the main window:
use strict;
use warnings;
use Tk;
my $mw = MainWindow->new;
$mw->Button (-text => "Hello World!", -command => sub {$mw->destroy()})->pack;
MainLoop;
print "done with the GUI\n";
Prints:
done with the GUI
If what you want is to put little dialogs windows in various places and then continue after they are handled, then that is easy. Here is something adapted from some code I am working on right now:
#!/usr/bin/perl -w
use strict;
use warnings;
use Tk;
my $mw = MainWindow->new();
$mw->withdraw();
my $ftp_warn = $mw->messageBox(
-title => 'Silly message',
-message => "We are displaying a silly message, do you wish to continue?",
-type => 'YesNo',
-icon => 'question',
);
if ( $ftp_warn eq 'No' ) {
exit;
}
else {
my $msg2 = $mw->messageBox(
-title => 'Really?',
-message => "We displayed silly message and you wish to continue?",
-type => 'OK',
-icon => 'question',
);
exit;
}
That code will run.
Now if you were to replace the else clause with a call to a function, then you can execute other code. This is naturally quite trivial, because in a Tk application you will have some sort of user interface that appears in the MinWindow (the $mw that I hid with the $mw->withdraw method call) whioch will direct what is to be done.
Tk is a very useful GUI tool, there are a number of heavy Tk users here in the Monastery. You will find it explained in the docs or you could look at Mastering Perl/Tk" by Lidie and Walsh published by O'Reilly.
[jdtoronto]
This node could also be interesting for you: Tk - intercepting alt-f4 and the like
Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"
perlmonks.org content © perlmonks.org and GrandFather, herby1620, jdtoronto, strat
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03