Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Tk Update

by dabella12 (Initiate)
on Apr 22, 2013 at 15:23 UTC ( #1029906=perlquestion: print w/ replies, xml ) Need Help??
dabella12 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks I need a little help :) I'm writing a script that needs to update a label with the current time. here is the code I'm using but it doesn't work

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Tk; use Date::Manip; my $time=&UnixDate('now',"The time is: %r on %B %d, %Y"); my $mw=MainWindow->new(-title=>'Time Test'); my @top = qw/-side top -fill both -padx 2 -pady 2 -expand 1/; my @left = qw/-side left -fill both -padx 2 -pady 2 -expand 1/; my $frm=$mw->Frame(-bd =>2); my $frm1=$mw->Frame(-bd =>2); my $oper=$frm->Button( -text => "Entrada De Operaciones", -command => sub{print "Hello World\n"}, ); my $client =$frm->Button( -text => 'Informe De Clientes', -command => sub{print "Hello World\n"}, ); my $general =$frm->Button( -text => 'Informe General', -command => sub{print "Hello World\n"}, ); my $util =$frm->Button( -text => 'Utilidades', -command => sub{print "Hello World\n"}, ); my $exit =$frm->Button( -text => 'Exit Application', -command => sub{exit}, ); my $lb=$mw->Label(-text=>\&update_time)->pack(@top); $frm->pack(@top); $oper->pack(@top); $client->pack(@top); $general->pack(@top); $util->pack(@top); $exit->pack(@top); MainLoop; sub update_time { $time=&UnixDate('now',"The time is: %r on %B %d, %Y"); print "$time\n"; $lb->configure(-text=>"$time"); $mw->update; $mw->after(1); }

This doesn't work for some reason.

Comment on Tk Update
Download Code
Re: Tk Update
by CountOrlok (Friar) on Apr 22, 2013 at 15:44 UTC
    Replace your my $lb line with:
    my $lb=update_time($mw)->pack(@top);
    and you update_time subroutine with:
    sub update_time { my $mw = shift; my $time=UnixDate('now',"The time is: %r on %B %d, %Y"); my $lbl = $mw->Label(-text=>"$time"); $mw->update; $mw->after(1); return $lbl; }

      The function still does not update the time

        How about this?:

        use strict; use warnings; use Tk; use Date::Manip; my $time = UnixDate('now',"The time is: %r on %B %d, %Y"); my $mw = MainWindow->new( -title => 'Time Test' ); my @pack_opts = qw/-fill both -padx 2 -pady 2 -expand 1/; my $frm = $mw->Frame( -bd => 2 )->pack; my $lbl = $frm->Label( -text => $time )->pack(@pack_opts); my $oper = $frm->Button( -text => "Entrada De Operaciones", -command => sub { print "Hello World\n" }, )->pack(@pack_opts); my $client = $frm->Button( -text => 'Informe De Clientes', -command => sub { print "Hello World\n" }, )->pack(@pack_opts); my $general = $frm->Button( -text => 'Informe General', -command => sub { print "Hello World\n" }, )->pack(@pack_opts); my $util = $frm->Button( -text => 'Utilidades', -command => sub { print "Hello World\n" }, )->pack(@pack_opts); my $exit = $frm->Button( -text => 'Exit Application', -command => sub { $mw->destroy }, )->pack(@pack_opts); $mw->repeat( 1000 => sub { update_time() } ); MainLoop; sub update_time { my $time = UnixDate( 'now', "The time is: %r on %B %d, %Y" ); $lbl->configure( -text => $time ); }

        Or even better, with Tk::StrfClock:

        use strict; use warnings; use Tk; use Tk::StrfClock; my $mw = MainWindow->new( -title => 'Time Test' ); my @pack_opts = qw/-fill both -padx 2 -pady 2 -expand 1/; my $frm = $mw->Frame( -bd => 2 )->pack; $frm->StrfClock()->pack(@pack_opts); my $oper = $frm->Button( -text => "Entrada De Operaciones", -command => sub { print "Hello World\n" }, )->pack(@pack_opts); my $client = $frm->Button( -text => 'Informe De Clientes', -command => sub { print "Hello World\n" }, )->pack(@pack_opts); my $general = $frm->Button( -text => 'Informe General', -command => sub { print "Hello World\n" }, )->pack(@pack_opts); my $util = $frm->Button( -text => 'Utilidades', -command => sub { print "Hello World\n" }, )->pack(@pack_opts); my $exit = $frm->Button( -text => 'Exit Application', -command => sub { $mw->destroy }, )->pack(@pack_opts); MainLoop;

        Regards, Stefan

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1029906]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2014-09-20 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls