Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

TK textvariable not updating

by Stoney2005 (Acolyte)
on May 28, 2011 at 19:14 UTC ( #907154=perlquestion: print w/replies, xml ) Need Help??
Stoney2005 has asked for the wisdom of the Perl Monks concerning the following question:

So I asked this in the chatterbox. tye helped me try and debug it but i'm just not having much luck. with perl tk you can tie a label's text to a variable and when the variable changes it will update the label's text. sounds nice right? well its not working so well for me. 1st try
#!/usr/bin/env perl use Tk; use Time::Stopwatch; $mw = new MainWindow; #Initialize main window $ongoing_time = 0; my $label_running_time_time = $mw -> Label(-textvariable=> \$ongoing_t +ime, -relief=>'groove') -> grid(-row=>9,-column=>2); # bind with tie: +:watch my $button_start_recording = $mw -> Button(-text => "Start", -command +=>\&start_recording_button) -> grid(-row=>10,-column=>1); # duuh my $button_stop_recording = $mw -> Button(-text => "Stop", -command => +\&stop_recording_button) -> grid(-row=>10,-column=>3); # duhh sub start_recording_button {tie $ongoing_time, 'Time::Stopwatch';} sub stop_recording_button {print "Time: $ongoing_time\n";} MainLoop;
I also tried using pack instead of grid, telling $mw-> update(); in the subs, i tried telling $label_running_time_time to update in the subs. I'm kinda at a loss for how to fix it. i used this for reference. specifically the pathentry widget. any help would be appreciative. Also not sure if it will make a difference but i'm running perl v5.10.1 on x86_64-linux-gnu-thread-multi on debian 2.6.38-2-amd64

UPDATE:: It appears that tye was right the variable $ongoing_time is not updated until it's called specifically. crap. anyone have a idea for initiating a timer?

UPDATE:: I found a solution. I'd like it in a straight string tho.
use Tk; use Time::Duration; $start_time = time(); print "Start Time: $start_time\n"; $mw = MainWindow->new(); $timi = $mw->Label( -textvariable=>\$elapsed_time, -foreground=>'blue' + )->pack; $mw->Button( -text=>'Quit', -command=>sub{ exit } )->pack; $mw->Button( -text=>'Update', -command=>sub{ print "New Time: $elapsed +_time\n"; } )->pack; sub update_time {$elapsed_time = concise(duration_exact(time() - $star +t_time)); $timi->update();} $timi->repeat(50,\&update_time); MainLoop;
In a bit if i can find a new one then i'll post it

UPDATE:: I'm happy for the update function. The new code is use Time::Format; sub update_time {$elapsed_time = time_format('mm:ss', (time() - $start_time));$timi->update(); $progress->value((time() - $start_time));}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://907154]
Approved by Corion
Front-paged by Corion
[stevieb]: been thinking... if it wasn't for Perl, I believe that I'd be done in tech. I don't do social media or the like, if I'm not coding I'm not on a device at all, and don't miss comms at all when I'm not around. I code for my job (which I like), but I think..
[stevieb]: ...I haven't coughed up my laptop because of Perl.
[stevieb]: I think that Perl is the reason that I haven't gone 100% off grid :)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2017-03-29 02:12 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (343 votes). Check out past polls.