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

Greetings monks, My 1st Tk project. Have searched the archives, prior questions and the web. There are hints but the answer is just a bit out of reach. Synopsis - Scrolled text widget doesn't show simple output.
pseudo-snippet -- use Tk; $mw = MainWindow->new; $tw = $mw->Scrolled ("Text"); $bu = $mw->Button(-text=>"Go", -command=>\&dosomething); Mainloop; exit; sub dosomething { for my $i (1 .. 500) { $tw->insert('end',"\$i is " . $i); }; } __END__
Nothing displays in the widget until the end of dosomething. How can I update it dynamically? I've tried using 'see', 'fileevent' and 'focus' on $tw and have $|++'ed liberally but still get no output until the loop completes :-( dooj

Replies are listed 'Best First'.
Re: Tk Scrolled widget hairpuller
by zentara (Cardinal) on Apr 22, 2005 at 22:05 UTC
    You should always post a working snippet, or else say it is pseudocode. You have a couple of errors in the script, like Mainloop should be MainLoop. You should always use strict and warnings, which would have told you that.

    Anyways, back to your real question... you need to pack your widgets and update your text widget with each print.

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; my $tw = $mw->Scrolled ("Text")->pack; my $bu = $mw->Button(-text=>"Go", -command=>\&dosomething)->pack; MainLoop; exit; sub dosomething { for my $i (1 .. 500) { $tw->insert('end',"$i is $i\n" ); $tw->update; $tw->see('end'); select(undef,undef,undef,.1); }; } __END__

    I'm not really a human, but I play one on earth. flash japh
      Greetings Monk Zentara, Thank you for the informative and helpful reply. The 'update' statement was the one thing that had eluded me in my copy of O'Reilly Learning Perl/Tk. Adding it per your advice created the desired result. I bow before the gates, Dooj