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

I want to be able to print text to a text widget immediately. So in the below code example, what ends up happening is both text items get printed after the sleep. I want the print that occurs before the sleep to actually happen, then sleep, then print what's after the sleep. Do I need to utilize fileevent for this? Can someone give me an example on how to do this? Many thanks!
use Tk; $mw = new MainWindow; $mw->title("Test"); $mw->Label(-text => "Pick an item from the list")->pack; $frm_versions=$mw->Frame()->pack; $list=$frm_versions->Listbox(-selectmode=>'single', -width=>0, -height +=>0); $list->pack; $list->insert('end', 'Text', 'MoreText', 'TextityTextText', 'TextyText +alicious', 'Textathon'); $textarea=$mw->Frame()->pack; $txt=$textarea->Scrolled("Text", -scrollbars=>'e', -width=>45, -height +=>10); $txt->pack; $mw->Button(-text => "GO!!!!", -command => \&go)->pack(-side => 'botto +m', -expand => 1, -fill => 'x'); MainLoop(); sub go{ $listselection=$list->curselection(); if ($listselection eq "") { $todo = ""; }else { $todo=$list->get($listselection); } $txt -> delete('0.0', 'end'); if ($todo){ $txt -> insert('end',"Let's see what you picked...\n\n\n"); sleep (2); $txt -> insert('end',"You picked $todo...\n"); }else{ $txt->insert('end',"Please pick something from the list...\n"); last; } }

Replies are listed 'Best First'.
Re: Unbuffered printing to tk text widget
by zentara (Cardinal) on May 23, 2007 at 15:40 UTC
    To solve your immediate problem:
    $txt -> delete('0.0', 'end'); if ($todo){ $txt -> insert('end',"Let's see what you picked...\n\n\n"); $txt->update;
    However, you need to stop using sleep in a gui program. Why? Because what if you have other things going on in the background, in a more complex script? sleep will stop them all from functioning for 2 seconds. sleep puts the WHOLE script to sleep. Use this:
    # sleep (2); $mw->after(2000); #milliseconds

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      thank you both! the update statement worked!! The sleep was just in there as an example.. to represent "do stuff"... I don't actually have a sleep in my script. thanks again!!
Re: Unbuffered printing to tk text widget
by johngg (Canon) on May 23, 2007 at 15:23 UTC
    If memory serves, there's a $widget->update() method that you might be able to apply.

    Cheers,

    JohnGG