Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to do here is to enter the time in first field and when I click on the Start button the time would be set by the value entered and started on top, showing today's date, time started and time left. I could also overwrite the value and by clicking again the timer would pickup from there. Now when a click on the stop button, I would like to set that value on the second entry box with the value of the first entry box, more like a pause button function. I am also thinking if it's possible to have some kind of warning to advise that the time is about to expire. I am new to this and if there is someone there to help me it would be very helpfull.
Thank you very much
Here is the code I have so far.

#! /perl/bin/perl5 -w use strict; use Tk; use Time::HiRes qw ( gettimeofday tv_interval ) ; my ( $mw , $msg , $msg2 , $mlabel , $id , $bf , $stime, $font_val) ; my @bgc =( "#000000"); my $main = MainWindow->new; $mlabel = $main->Label ( -textvariable => \$msg )-> pack ( -side => "t +op" ) ; $main->Label(-text => 'Enter Time')->pack; my $font = $main->Entry(-width => 20); $font->pack; my $filename = $main->Entry(-width => 20); $filename->pack; #test #$font = $main->Label(-textvariable => \$font_val);$font->pack; ## $main->Button(-text => 'Start', -command => sub{do_time($filename, $font)} )->pack( -side => "left", -padx => 5 ) ; $main->Button(-text => 'Print', -command => sub{do_print($filename, $font)} )->pack( -side => "left" , -padx => 5 ) ; $main->Button ( -text => "Exit" , -command => \&on_done )->pack ( -sid +e => "left", -padx => 5 ) ; $mlabel = $main->Button ( -text => "Start Timer" , -command => \&on_st +art )->pack ( -side => "left", -padx => 5 ) ; $main->Button ( -text => "Stop" , -command => \&on_stop )->pack ( -sid +e => "left" , -padx => 5 ) ; $main->Label(-textvariable => \$font_val)->pack( -side => "top" ); MainLoop; sub do_time { my ($file, $font) = @_; my $file_val = $file->get; my $font_val = $font->get; print "Now Time is $file_val in $font_val\n"; $main->Label(-textvariable => \$font_val)->pack( -side => "top" ); + } sub do_print { my ($file, $font) = @_; my $file_val = $file->get; my $font_val = $font->get; print "Sending file $file_val to printer in $font_val\n"; } sub on_done() { exit ; } sub on_timer() { my $t = time ; my @d = localtime $t ; my $elapsed; my $bc; $msg = sprintf ( "%04d-%02d-%02d %02d:%02d:%02d" , $d[5]+1900 , $d[4]+ +1 , $d[3] , $d[2] , $d[1] , $d[0] ) ; $elapsed = tv_interval ( $stime ) ; $msg = sprintf ( "%0.2f" , $elapsed ) ; # #my $bc = $bgc[ int($elapsed) % 8 ] ; #$main->configure ( -background => $bc ) ; #$bf->configure ( -background => $bc ) ; #return if $id ; #$stime = [gettimeofday] ; #$id = $mlabel; } sub on_start() { return if $id ; $stime = [gettimeofday] ; $id = $mlabel->repeat ( 50 , \&on_timer ) ; } sub on_stop() { return unless $id ; $id->cancel() ; $id = undef ; }

Replies are listed 'Best First'.
Re: Computer User Timer
by bobn (Chaplain) on Jul 03, 2003 at 23:31 UTC
    Your description of what you want to do is hard to understand. Also, in light of your description, the progam is bizarre as the input fields get used for filenames and fonts, which are nowhere mentioned in your description.

    Very odd.

    --Bob Niederman, http://bob-n.com