Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#! /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 |