Gossamer Forum
Home : General : Perl Programming :

Quick Perl/Tk question! Please help! Please!

Quote Reply
Quick Perl/Tk question! Please help! Please!
Hey Guys/Gals,

The following program displays the amount of Days Hours Minutes and Seconds till Friday 5pm. THen there's a update button which you hit and it updates the amount of time. I want to get rid of the Update button and have the Label widget update by itself, so you actually see the seconds and stuff counting down.. Can someone please help !#%? It's driving me crazy that i cant figure how to do it. Thanks!

-------------------------------------------------------------------

#!/usr/local/bin/perl

use Tk;
use POSIX;


my $mw = MainWindow->new();
$mw->geometry("+0-0");
$mw->title('Countdown to Friday 5pm');


sub count {

$| = 1;

while(1) {

my $now = time();
my $weekend = $now;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime( $now );
$weekend -= ( $sec );
$weekend -= ( $min * 60 );
if ( $hour >= 17 ) {
$weekend -= ( $hour - 17 ) * 60 * 60;
} else {
$weekend += ( 17 - $hour ) * 60 * 60;
}
if ( $wday >= 5 ) {
$weekend -= ( $wday - 5 ) * 60 * 60 * 24;
} else {
$weekend += ( 5 - $wday ) * 60 * 60 * 24;
}
my $difference = abs( $weekend - $now );
my $wait_days = floor( $difference / ( 60 * 60 * 24 ) );
$difference -= $wait_days * 60 * 60 * 24;
my $wait_hours = floor( $difference / ( 60 * 60 ) );
$difference -= $wait_hours * 60 * 60;
my $wait_minutes = floor( $difference / 60 );
$difference -= $wait_minutes * 60;
my $wait_seconds = $difference;

return sprintf "\r %02dd %02dh %02dm %02ds", $wait_days, $wait_hours, $wait_minutes, $wait_seconds;

};
};


sub update {
&count;
$text = &count;
}

&count;
$text = &count;
$label = $mw->Label(-textvariable => \$text)->pack(-side => 'left');


$CHECK = $mw->Button(-text => 'Update',
-foreground => 'red',
-background => 'black',
-anchor => 'center',
-relief => 'groove',
-command => \&update)->pack();

MainLoop;
Quote Reply
Re: [mtorres] Quick Perl/Tk question! Please help! Please! In reply to
What actually is Perl/Tk?...I don't know :( ...do you have an example script online anywhere?

Sorry, I bet you were expecting an answer to your question!
Quote Reply
Re: [Paul] Quick Perl/Tk question! Please help! Please! In reply to
TK is an add on module for perl. It lets you make GUI programs using perl code. It's pretty simple, besides for stupid little problems like the one i'm having. It's fun to play around with. you should try...

perl -e 'use Tk;'

If you dont recieve any errors. Then you have TK installed. If you do recieve errors, then you dont.

p.s. , yes, i was expecting to see an answer to my question. hehe. later.

also.... copy and past the code in my thread above to a perl file. Try running it, thats an example of a perl/tk program. i also have www.ninjapants.org/files/tmp-source/sys.txt , which is another example of a perl/tk script.
Quote Reply
Re: [mtorres] Quick Perl/Tk question! Please help! Please! In reply to
Ohhh...that's cool!

I just tried your test script. I never knew you could do that...heh.

Thanks!
Quote Reply
Re: [mtorres] Quick Perl/Tk question! Please help! Please! In reply to
I have no idea how to use Tk as you know so don't take my word for it, but you may need to redraw the button inside your while loop so the time keeps updating?
Quote Reply
Re: [mtorres] Quick Perl/Tk question! Please help! Please! In reply to
Change



$CHECK = $mw->Button(-text => 'Update',
-foreground => 'red',
-background => 'black',
-anchor => 'center',
-relief => 'groove',
-command => \&update)->pack();

to

while (1) {
&update;
$mw->update();
}


That will do what you want..