For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2007 > Perl/Tk Help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Perl/Tk Help
CM Analyst

2007-09-18, 7:02 pm

Hi All,

I hope this is still the correct forum to get help
with Perl/Tk...

My goal is display the time in the GUI window but it
doesn't do it quite how I want it to.

In the GUI, I want the time to display with the sleep
call (set to 1). However, when I use the print
statement and the value goes to STOUT, the time value
displays as I want it to.

Can someone please tell me what I am missing here?
Thank you,

#########
use Tk;
use Time::Local;

my $main = MainWindow->new;

$main->Button (-text => 'Start', -command =>
\&display) -> pack (-side => "left")
-> pack (-anchor => "n");
$text1 = $main->Text ('-width' => 30, '-height' => 15,
'-background' => Grey,)
-> pack;
$main->Button(-text => 'Stop', -command => [$main =>
'destroy'])->pack;

sub display {

for (my $index = 0; $index <= 5; $index++ ) {
my ($sec, $min, $hour, $mday, $mon, $year, $wday,
$yday, $isdst) = localtime();

sleep 1;

#$text1->insert('end', "$hour:$min:$sec\n");

print ("$hour:$min:$sec\n");

}
}

MainLoop;




________________________________________
________________________________________
____
Don't let your dream ride pass you by. Make it a reality with Yahoo! Autos.
http://autos.yahoo.com/index.html



Patmarbidon

2007-09-19, 4:01 am

PatMar :

Can you add $text1->update() after your $text1->insert.

widget->update is like a refresh on other X11 windows manager.


CM Analyst a écrit :
> Hi All,
>
> I hope this is still the correct forum to get help
> with Perl/Tk...
>
> My goal is display the time in the GUI window but it
> doesn't do it quite how I want it to.
>
> In the GUI, I want the time to display with the sleep
> call (set to 1). However, when I use the print
> statement and the value goes to STOUT, the time value
> displays as I want it to.
>
> Can someone please tell me what I am missing here?
> Thank you,
>
> #########
> use Tk;
> use Time::Local;
>
> my $main = MainWindow->new;
>
> $main->Button (-text => 'Start', -command =>
> \&display) -> pack (-side => "left")
> -> pack (-anchor => "n");
> $text1 = $main->Text ('-width' => 30, '-height' => 15,
> '-background' => Grey,)
> -> pack;
> $main->Button(-text => 'Stop', -command => [$main =>
> 'destroy'])->pack;
>
> sub display {
>
> for (my $index = 0; $index <= 5; $index++ ) {
> my ($sec, $min, $hour, $mday, $mon, $year, $wday,
> $yday, $isdst) = localtime();
>
> sleep 1;
>
> #$text1->insert('end', "$hour:$min:$sec\n");
>
> print ("$hour:$min:$sec\n");
>
> }
> }
>
> MainLoop;
>
>
>
>
> ________________________________________
________________________________________
____
> Don't let your dream ride pass you by. Make it a reality with Yahoo! Autos.
> http://autos.yahoo.com/index.html
>
>
>
>


Zentara

2007-09-19, 8:00 am

On Tue, 18 Sep 2007 14:42:09 -0700 (PDT), cmanalyst@yahoo.com (CM
Analyst) wrote:

>In the GUI, I want the time to display with the sleep
>call (set to 1). However, when I use the print
>statement and the value goes to STOUT, the time value
>displays as I want it to.
>
>Can someone please tell me what I am missing here?
>Thank you,


>sub display {
>
>for (my $index = 0; $index <= 5; $index++ ) {
>my ($sec, $min, $hour, $mday, $mon, $year, $wday,
>$yday, $isdst) = localtime();


Don't use sleep in a gui, unless you really know what you
are doing, use a timer. Sleep "blocks" the eventloop.
>sleep 1;
>
>#$text1->insert('end', "$hour:$min:$sec\n");
>
>print ("$hour:$min:$sec\n");
>
>}
>}


#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Time::Local;

my $main = MainWindow->new;

$main->Button (-text => 'Start', -command =>
\&display) -> pack (-side => "left")
-> pack (-anchor => "n");

my $text1 = $main->Text ('-width' => 30, '-height' => 15,
'-background' => 'Grey',)-> pack;

$main->Button(-text => 'Stop', -command => [$main =>
'destroy'])->pack;

MainLoop;

sub display {

my $timer = $main->repeat(1000, sub{

my ($sec, $min, $hour, $mday, $mon, $year, $wday,
$yday, $isdst) = localtime();

$text1->insert('end', "$hour:$min:$sec\n");
});

}
__END__

zentara


--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com