Well, for starters, in Tk land, anything you do before you execute MainLoop is completed (in most cases) before you see anything displayed. I would add a start button to the widget that starts the loop. Then I would get away from sleep and the for..next. I would use a timer (GUIs should *almost* never block) that repeatedly calls the subroutine that handles the counting and printing.

If you do this, then the loop will not be started until the GUI is up. And your display should be updated since $widget->after() does not put the interface (GUI) to sleep.

I have tested the following modifications, and they work on my system...

#!/usr/local/bin/perl use strict; use Tk; my $Mycount = 0; my $submain = MainWindow->new(); my $buttons = $submain->Frame()->pack(-side => 'top'); my $status = $buttons->Button(-text => 'Click to Clear', -command => sub {$submain->destroy;} )->pack(-side=>'left'); my $text1 = $submain->Text ('-width'=>100, 'height'=>20); $text1->pack(-side => 'left', -fill => 'y'); my $doit = $buttons->Button(-text => 'Do It', -command => [ \&theLoop => $submain, \$Mycount], )->pack(-side=>'left'); $text1->insert('end', "start\n"); $text1->pack; MainLoop(); sub theLoop { my $widget = shift; my $Mycount = shift; my $line = ''; $$Mycount++; #for(my $loop=0; $loop <= 3; $loop++ ) ## Looping handled by timer +... #{ #sleep 1; Incompatible with Tk MainLoop - It pauses the *enti +re* application print "$$Mycount\n"; if ( $$Mycount == 1 ) { $line = "$$Mycount case 1\n"; } elsif ( $$Mycount == 2 ) { $line = "$$Mycount case 2\n"; } else { $line = "$$Mycount something else\n"; } $text1->insert('end', "$line"); #$text1->pack; #} ## Here is where we implement the delay.... and the loop ## We simply check to see if our loop max has been reached, then i +f not, ## set a future event to call this subroutine again. ## The 500 is the number of milliseconds to wait before calling. ## This is better programming in Tk, since the sleep command stops + everything ## which is very bad with a GUI. (think M$ when a low level call h +appens) if ($$Mycount < 3) { my $delay = $submain->after(500, [\&theLoop => $widget, $Mycou +nt]); } }

In reply to Re: Refresh text widget by Anonymous Monk
in thread Refresh text widget by MadMax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.