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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a Tk expert 1

Status
Not open for further replies.

timtrust123

Programmer
Dec 19, 2003
19
GB
I would like the text on a button to update while a sub is running. I have linked the button text to a variable in the example below hoping it would update during the sub but it does not. How can I update the button text during the sub running. (i'm using win NT )

use Tk;
$info = "start";
$mw = MainWindow->new;
$i = 0;
$b = $mw->Button(-text=> $info)->pack();
$b->configure (-command => \&push_button);

MainLoop;


sub push_button
{
$i = 0;
while ($i < 1000000){$i++}
$info = "first stage";
$i = 0;
while ($i <100000){$i++}
$info = "second stage";
$i = 0;
while ($i <100000){$i++}
$info = "third stage";
print "done\n$info";
}
 
I have the same problem updating a label
Any ideas out there????
 
Hi Tim

To update the text on the button when pressed you need to reconfigure yot button ... to do this you should change your sub-routine to the following (or equivalent):

Code:
sub push_button
{
++$counter;
$b->configure (-text=>"Button Press: $counter");
}

Remember that Tk is event driven so having the button update dynamically over time will require a bit more effort ... you should read the following sample from the O'Reilly Web Site regarding the Anatomy of the MainLoop for more information...


hope this helps ...
 
Hi tim,

Rab54's thread, thread219-875531 ... got me thinking, if you update your code to the following (or similar) then the button will update as required:

Code:
use Tk;
$info = "start";
$mw = MainWindow->new;
$i = 0;
$b = $mw->Button(-text=> $info)->pack();
$b->configure (-command => \&push_button);

MainLoop;


sub push_button
{
$i = 0;
while ($i < 1000000){$i++}
[b][COLOR=red]$b->configure(-text=>"first stage");
$b->update;[/color][/b]
$i = 0;
while ($i <10000000){$i++}
[b][COLOR=red]$b->configure(-text=>"second stage");
$b->update;[/color][/b]
$i=0;
while ($i <1000000){$i++}
[b][COLOR=red]$b->configure(-text=>"third stage");
$b->update;[/color][/b]
}

Basically the button widget ($b) is being refreshed after every while loop...

hope this helps
 
Many thanks to Parkers.
This is exactly what I wanted. I very much appreciate you taking time to read my posting and suggesting the solution.
Many thanks....
 
glad to help ...

I'm interested in developing my Tk skills also !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top