Code Comments
Programming Forum and web based access to our favorite programming groups.Here's my problem: I need to keep running a subroutine (with a while
loop) until a condition is met, but this condition can only be set by
a button on my Tk interface being pressed. The problem is that while
the loop is running no other activity in the application is allowed,
buttons can't be pressed etc. All of the methods I've seen so far
haven't worked out. For the purposes of this problem I'll provide the
main structure of my program (without the progran-specifc
subroutines), I hope this gives you a clear idea of what I want to
acheive.:
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
my $fill = 0;
my $labtext = 1;
my $mw = MainWindow->new;
$mw->title("Loop test");
$mw->geometry('600x400');
my $startbut =
$mw->Button( -text => 'Start filling', -command => \&start_filling )-
>pack();
my $stopbut =
$mw->Button( -text => 'Stop filling', -command => \&stop_filling )-
>pack();
my $lab = $mw->Label( -textvariable => \$labtext )->pack;
MainLoop;
sub start_filling {
$fill = 1;
while ( $fill == 1 ) {
$labtext++;
print $labtext;
$mw->idletasks;
$mw->after(50);
}
}
sub stop_filling {
$fill = 0;
}
Post Follow-up to this messageOn Tue, 18 Mar 2008 04:28:20 -0700 (PDT), disco
<discombobulated244@gmail.com> wrote:
>Here's my problem: I need to keep running a subroutine (with a while
>loop) until a condition is met, but this condition can only be set by
>a button on my Tk interface being pressed. The problem is that while
>the loop is running no other activity in the application is allowed,
>buttons can't be pressed etc. All of the methods I've seen so far
>haven't worked out. For the purposes of this problem I'll provide the
>main structure of my program (without the progran-specifc
>subroutines), I hope this gives you a clear idea of what I want to
>acheive.:
>sub start_filling {
> $fill = 1;
>
> while ( $fill == 1 ) {
> $labtext++;
> print $labtext;
>
> $mw->idletasks;
> $mw->after(50);
>
> }
>
>}
>
>sub stop_filling {
> $fill = 0;
>}
You are probably stopping the eventloop from functioning
in your while() loop. While() loops are very bad design when using
gui's. You can try to put a $mw->update and remove the after(50).
Instead of the while($fill==1){..... $mw->after(50)...... }
you should serup a repeater, like this:
in main program:
$mw->repeat(50, \&start_fill);
sub start_fill{
if( $fill == 1 ){
$labtext++;
print $labtext;
$mw->update;
}else{return}
}
Much really depends on the program you are trying to run in the
start_filling sub. Is it an external program? If so, you are probably
best off running it in a thread, and control it thru shared variables.
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Post Follow-up to this messageOn Mar 18, 1:17 pm, zentara <zent...@highstream.net> wrote:
> You are probably stopping the eventloop from functioning
> in your while() loop. While() loops are very bad design when using
> gui's. You can try to put a $mw->update and remove the after(50).
>
> Instead of the while($fill==1){..... $mw->after(50)...... }
> you should serup a repeater, like this:
>
> in main program:
>
> $mw->repeat(50, \&start_fill);
>
> sub start_fill{
>
> if( $fill == 1 ){
> $labtext++;
> print $labtext;
> $mw->update;
> }else{return}
>
> }
>
> Much really depends on the program you are trying to run in the
> start_filling sub. Is it an external program? If so, you are probably
> best off running it in a thread, and control it thru shared variables.
>
> zentara
>
> --
> I'm not really a human, but I play one on earth.http://zentara.net/japh.html[/colo
r]
Thanks very much, this works great! Here's the completed code in case
anybody comes across the same problem:
#!/usr/bin/perl
use strict;
use warnings;
use Tk;
my $fill = 0;
my $labtext = 0;
my $mw = MainWindow->new;
my $counter;
$mw->title("Loop test");
$mw->geometry('600x400');
my $startbut = $mw->Button(
-text => 'Start filling',
-command => sub { $fill = 1; $counter = $mw->repeat( 50,
\&start_fill ); }
)->pack();
my $stopbut =
$mw->Button( -text => 'Stop filling', -command => \&stop_filling )-
>pack();
my $clear =
$mw->Button( -text => 'Clear', -command => sub {&stop_filling;
$labtext = 0; })->pack();
my $lab = $mw->Label( -textvariable => \$labtext )->pack;
MainLoop;
sub start_fill {
if ( $fill == 1 ) {
$labtext++;
print $labtext;
# Note: this causes a millisecond or two of lag, it can be
disabled in this context if necessary
$mw->update;
}
else {
return;
}
}
sub stop_filling {
$fill = 0;
$counter->cancel;
}
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.