For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > June 2006 > Can't "last" outside a MainLoop block









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can't "last" outside a MainLoop block
squan

2006-06-25, 7:01 pm

NOTE: TK novice here, Old X-windows R3/R4 developer

I'm trying to provide a non-window interface to plug in to existing
perl scripts. So, MainLoop has to be exited after user interaction is
finished.

The code goes like this
....(widget creation to interact with user)
.... -command => sub{last}; # line 265, trying to jump out of MainLooop
.....
$mw->MapWindow();
MainLoop;

<continue with main program>

That works fine on Unix (solaris & hpux), but in Microsoft Windows
(Perl v5.8.7 built for MSWin32-x86-multi-thread), right after clicking
the same button I get (on stderr)

Tk::Error: Can't "last" outside a loop block at guitk2.pm line 265.
Tk callback for .button
Tk::__ANON__ at c:/Program Files/apache2/Perl/site/lib/Tk.pm line 247
Tk::Button::butUp at c:/Program
Files/apache2/Perl/site/lib/Tk/Button.pm line 111
<ButtonRelease-1>
(command bound to event)

How do I jump out of MainLoop and stop the MainLoop event loop on both
Unix and Windows?

thanks in advance

Marc Dashevsky

2006-06-25, 7:01 pm

squan <squan@portal.com> writes in article %:
> NOTE: TK novice here, Old X-windows R3/R4 developer
>
> I'm trying to provide a non-window interface to plug in to existing
> perl scripts. So, MainLoop has to be exited after user interaction is
> finished.
>
> The code goes like this
> ...(widget creation to interact with user)
> ... -command => sub{last}; # line 265, trying to jump out of MainLooop
> ....
> $mw->MapWindow();
> MainLoop;
>
> <continue with main program>
>
> That works fine on Unix (solaris & hpux), but in Microsoft Windows
> (Perl v5.8.7 built for MSWin32-x86-multi-thread), right after clicking
> the same button I get (on stderr)
>
> Tk::Error: Can't "last" outside a loop block at guitk2.pm line 265.
> Tk callback for .button
> Tk::__ANON__ at c:/Program Files/apache2/Perl/site/lib/Tk.pm line 247
> Tk::Button::butUp at c:/Program
> Files/apache2/Perl/site/lib/Tk/Button.pm line 111
> <ButtonRelease-1>
> (command bound to event)
>
> How do I jump out of MainLoop and stop the MainLoop event loop on both
> Unix and Windows?


$mw->destroy

--
Go to http://MarcDashevsky.com to send me e-mail.
squan

2006-06-25, 7:01 pm

wm->destroy() will destroy $wm. I would like to reuse it.

If no other solution, I would have to re-create $wm everytime. But for
the moment, I would like to keep the main window for a possible set of
different user interface popups.

Petr Vileta

2006-06-25, 7:01 pm

squan wrote:
> NOTE: TK novice here, Old X-windows R3/R4 developer
>
> I'm trying to provide a non-window interface to plug in to existing
> perl scripts. So, MainLoop has to be exited after user interaction is
> finished.
>
> The code goes like this
> ...(widget creation to interact with user)
> ... -command => sub{last}; # line 265, trying to jump out of
> MainLooop ....
> $mw->MapWindow();
> MainLoop;
>
> <continue with main program>
>

I'm not sure if this is what you want but try to invert the process ;-)
I suppose that your current code is some like this:

---CUT---
some_perl_code
my $mw = MainWindow->new();
my $button = Button(....,-command => sub {last})->pack;
MainLoop;
other_perl_code

sub last {
.....
}
---CUT---

Try to write primary window interface and run "some_perl_code" and
"other_perl_code" as sub's.
To rewrite original source to new should be very easy. Mangle original
source to sub's as you want and write window interface to begin ;-)
At the begin of all sub's you can write some window like message to user
"Code ??? is running, please wait...". Label() widget is a good candidate
for this.

--
Petr

Skype: callto://fidokomik

Na mail uvedeny v headeru zpravy nema cenu nic posilat, konci to v PR*
:-) Odpovidejte na petr na practisoft cz

squan

2006-06-26, 4:02 am

Thanks
that will be another option.

My attempt is to hide the Gui/Tk stuffs.

The code used to be like:

my $input = askInput("what is your input?");

I just want to replace the askInput() to a be a Perl TK graphical
equivalent.

Changing the structure into a windows event loop will confuse everyone
here.
It's just the team dynammics. (Imagine the code review?)

Marc Dashevsky

2006-06-26, 4:02 am

squan <squan@portal.com> writes in article %:
> wm->destroy() will destroy $wm. I would like to reuse it.
>
> If no other solution, I would have to re-create $wm everytime. But for
> the moment, I would like to keep the main window for a possible set of
> different user interface popups.


MainLoop is nothing magic. It is simply the following code:

sub MainLoop
unless ($inMainLoop) {
local $inMainLoop = 1;
while (Tk::MainWindow->Count) {
DoOneEvent(0);
}
}
}

You could write something similar that pays attention to a variable
other than Tk::MainWindow->Count and call it rather than MainLoop.

--
Go to http://MarcDashevsky.com to send me e-mail.
Ch Lamprecht

2006-06-26, 4:02 am

squan wrote:

> The code used to be like:
>
> my $input = askInput("what is your input?");
>
> I just want to replace the askInput() to a be a Perl TK graphical
> equivalent.
>
> Changing the structure into a windows event loop will confuse everyone
> here.
> It's just the team dynammics. (Imagine the code review?)


code review?

use warnings;
use strict;
use Tk;
use Tk::DialogBox;

my $mw = MainWindow->new;
my $dialog = $mw->DialogBox( );

$dialog->add($_)->pack for qw/Label Entry/;
$mw->after(500,\&start_main);

MainLoop;

sub askInput{
my $text = shift;
$dialog->Subwidget('label')->configure(-text=>$text);
$dialog->Subwidget('entry')->delete(0,'end');
$dialog->Show();
return $dialog->Subwidget('entry')->get;
}

sub start_main{
main();
$mw->destroy;
}

#######

sub main{

#your code

my $input = askInput('what is your input');
print "$input\n";

#your code


}



Christoph
--

perl -e "print scalar reverse q/ed.enilno@ergn.l.hc/"
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com