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

Re: Updating Tk window in real time

by BrowserUk (Patriarch)
on Jul 21, 2005 at 22:05 UTC ( #477030=note: print w/replies, xml ) Need Help??

in reply to Updating Tk window in real time

Using DoOneEvent() with DONT_WAIT inside your get() sub allows the window to be built and updated in real time. (I've simulated your module with a random sleep)

use strict; #use mship; use Tk qw[ MainLoop DoOneEvent DONT_WAIT ]; my $host; $|++; my $mainwindow = new Tk::MainWindow(); my $text = $mainwindow->Scrolled( "Text", -scrollbars => 'se' )->pack( -expand => 1 , -fill => 'both' ); for ( 0 .. 53 ) { DoOneEvent( DONT_WAIT ) for 1 .. 100; ## process pending events; get($_); } # create a closebutton my $button = $mainwindow->Button( -text => "close", -command => sub{ $mainwindow->destroy() } )->pack(); # start the main loop! MainLoop; sub get { sleep rand 3; my $remote_name = "remote: $_[ 0 ]"; my $stats= rand() > 0.5 ? "Up" : "Down"; $text->insert( 'end', "$remote_name is $stats\n"); }

My choice of  for 1 .. 100; is somewhat arbitrary. It would be nice if you could say DoPendingEvents() or DoOneEvent() while EventsPending();, but I never found anything like that.

Update: Wadda ya know--seems DoPendingEvents() is called update():

use strict; #use mship; use Tk; my $host; $|++; my $mainwindow = new Tk::MainWindow(); my $text = $mainwindow->Scrolled( "Text", -scrollbars => 'se' )->pack( -expand => 1 , -fill => 'both' ); for ( 0 .. 53 ) { $mainwindow->update; get($_); } my $button = $mainwindow->Button( -text => "close", -command => sub{ $mainwindow->destroy() } )->pack(); MainLoop; sub get { sleep rand 3; my $remote_name = "remote: $_[ 0 ]"; my $stats= rand() > 0.5 ? "Up" : "Down"; $text->insert( 'end', "$remote_name is $stats\n"); }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://477030]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-10-03 14:12 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (14 votes). Check out past polls.