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

Hi,

My goal is it to see a button for 1 second and then it shall disappear. The following code is NOT working. It seems that the GUI is NOT updated after the first pack command. How can I force Perl/Tk to update the GUI after the first pack command?

#!/usr/bin/perl use Tk; my $mw = new MainWindow; my $button = $mw->Button('-text' => 'GO')->pack; sleep(1); $button->packForget(); MainLoop();
Thank you very much for your help
Dirk

Replies are listed 'Best First'.
Re: How to force Perl/Tk to update the GUI?
by zentara (Cardinal) on Nov 12, 2009 at 12:57 UTC
    ...ooh, you are missing the point of the eventloop..... you cannot put sleep into an eventloop gui. ..... what is happening, is that nothing gets displayed until the MainLoop line is reached....so when you write gui programs, remember...nothing gets displayed until the mainloop line is executed..... sleep interferes with mainloops....so setup timers
    #!/usr/bin/perl use warnings; use strict; use Tk; my $toggle; my $mw = new MainWindow; my $button = $mw->Button(-text => "RUN ", -bg => 'white', -command => \&toggle)->pack(-expand=>1, -fill=>'both'); my $timer = $mw->repeat(1000,\&toggle); #notice the difference #my $timer= $mw->after(1000,\&toggle); MainLoop; sub toggle{ print $button->cget('-text'),"\n"; if ( $button->cget('-text') eq 'RUN '){ $button->configure(-text => 'STOP', -bg => 'yellow', -activebackground => 'yellow', -highlightbackground => 'yellow' ); $toggle = 1; } elsif ( $button->cget('-text') eq 'STOP'){ $button->configure(-text => 'RUN ', -bg => 'white', -activebackground => 'white', -highlightbackground => 'white' ); $toggle = 0; } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: How to force Perl/Tk to update the GUI?
by keszler (Priest) on Nov 12, 2009 at 11:42 UTC
Re: How to force Perl/Tk to update the GUI?
by fmerges (Chaplain) on Nov 12, 2009 at 11:43 UTC

    Hi,

    I think for those sort of things you should use timed event, or signal in other GUI libs. Check out Tk::after

    Regards,

    fmerges at irc.freenode.net