Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl TK Buttons Not Redrawing During Subroutine Exec. 2

Status
Not open for further replies.

timwright

Technical User
Mar 7, 2004
1
0
0
US
Hello All,
I've got a program I'm writing using Perl/Tk. The whole point of the program is to go on the web fetch some stock information and return it. The program works execpt for a mere annoyance. While the program is retrieving the data I would like to have a label saying something along the lines of "Retrieving Data" and then clearing the message away after the data has been retrieved.. During the course of debugging and trying to figure out what was going on I found that not only is the message not displaying, but the button used to call the subroutines to get data, depresses and stays depressed until the subroutine exits. I wrote a smaller, simpler program to try and help debug the problem:
Code:
use strict;
use warnings;
use Tk;

my $main = MainWindow->new;
my $message = "Some text";
init();
MainLoop();

sub loopstuff()
{
   loading();
   my $count = 10000000;
   while($count > 0)
   {$count--;}
   done();
}
sub loading
{$message = "Loading";}

sub done
{$message = "Done";}

sub init
{
   my $frame = $main->Frame;
   $frame->Label(-textvariable => \$message)->pack();
   $frame->Button(-text => "ok", -command => \&loopstuff)->pack();
   $frame->Button(-text => "cancel", -command => \&exit)->pack();
   $frame->pack();
}

So what I would like to happen is when the button is pressed the message label changes to "Loading" until the counter counts all the way to 0, then the message changes to "Done". What happens when I run it is the button gets depressed and stays depressed until the counter has counted down then the message immediatly changes to "Done". I have put a "print "Made it here\n"" line in the loading subroutine and the program is executing that subroutine.

Thanks ahead of time for any help and I apologize if there are any problems with this post, this is the first time I've used a message board.

Thanks,
Tim
 
The problem is that widget are normally just updating during idle times. To force the update, run $widget->update. Here is your code modified to add the updates.

use strict;
use warnings;
use Tk;

my $main = MainWindow->new;
my $message = "Some text";
my $frame = $main->Frame;
my $Label = $frame->Label(-textvariable => \$message)->pack();
$frame->Button(-text => "ok", -command => [\&loopstuff, $Label])->pack();
$frame->Button(-text => "cancel", -command => \&exit)->pack();
$frame->pack();

MainLoop();

sub loopstuff($)
{
my $Widget = shift;
loading();
$Widget->update;
my $count = 10000000;
while($count > 0)
{$count--;}
done();
$Widget->update;
}
sub loading {$message = "Loading";}

sub done {$message = "Done";}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top