Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Breaking out of loops?
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;
}

Report this thread to moderator Post Follow-up to this message
Old Post
disco
03-18-08 01:08 PM


Re: Breaking out of loops?
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
zentara
03-19-08 12:09 AM


Re: Breaking out of loops?
On 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;
}

Report this thread to moderator Post Follow-up to this message
Old Post
disco
03-20-08 12:08 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PerlTk archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:50 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.