Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

[Tkx] Buffering/Updating issue for text element

by Gangabass (Priest)
on Aug 27, 2010 at 09:53 UTC ( #857650=perlquestion: print w/ replies, xml ) Need Help??
Gangabass has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks!

I have this code:

#!/usr/bin/perl use strict; use Tkx; my $mw = Tkx::widget->new('.'); $mw->g_wm_minsize( 400, 350 ); my $btn_start = $mw->new_ttk__button( -text => "Start", -width => 60, +-command => sub { start(); } ); my $txt_processed_domains = $mw->new_tk__text( -width => 40, -height = +> 10, -state => "disabled", -wrap => "none" ); Tkx::grid( $btn_start, -row => 2, -columnspan => 3, -padx => 10, -pady + => 10 ); Tkx::grid( $txt_processed_domains, -row => 3, -columnspan => 3, -padx +=> 10, -pady => 10 ); Tkx::MainLoop(); sub start { foreach my $id ( 1.. 10 ) { $txt_processed_domains->configure(-state => "normal"); $txt_processed_domains->insert_end( "$id => Available\n" ); $txt_processed_domains->configure(-state => "disabled"); sleep 1; } Tkx::tk___messageBox( -message => "Completed!" ); }

I need to see which ids are processed but only get whole list at the end. It's like buffering with filehandles but i'm not sure. How to see text in the text box just after inserting it?

Update: I find Tkx::update(); do what i want!

Comment on [Tkx] Buffering/Updating issue for text element
Select or Download Code
Re: [Tkx] Buffering/Updating issue for text element
by Anonymous Monk on Aug 27, 2010 at 10:05 UTC
    If you want your widgets to change while in callbacks, you have to update, ie $widget->update
      By widget you mean whole window or only text box? For text box ($txt_processed_domains->update;)
      bad option "update": must be bbox, cget, compare, configure, count, de +bug, delete, dlineinfo, dump, edit, get, image, index, insert, mark, +peer, replace, scan, search, see, tag, window, xview, or yview
      For window ($mw->update;):
      bad option "update": must be cget or configure
        That was perl-Tk-ism, for Tkx apparently it is Tkx::update();
Re: [Tkx] Buffering/Updating issue for text element
by zentara (Archbishop) on Aug 27, 2010 at 11:31 UTC
    I'm not a Tkx guru, but you should never use "sleep" in a GUI app, as it will interfere with the eventloop. As others have pointed out, you can hack around a sleep statement usage, with "$mw->updates" sprinkled around, but that is a poor way to write the code.

    Almost always, you avoid the problem by setting up a timer, rather than use sleep. Try taking the sleep(1) out of your code, and see if that speeds up the text inserts. A timer in Tkx would look something like the following.

    See Tkx repeat in case you are forced to use after to simulate repeat. I don't know if Tkx has a repeat

    # untested pseudocode sub start{ my @ids = (1..10); # repeat may not be available in Tkx.... see link above Tkx::repeat(1000, sub { my $id = shift @ids; if ($id <= 10){ $txt_processed_domains->configure(-state => "normal"); $txt_processed_domains->insert_end( "$id => Available\n" ); $txt_processed_domains->configure(-state => "disabled"); } else { Tkx::tk___messageBox( -message => "Completed!" ); } }); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks for Tkx::repeat but i use sleep only to show the issue. In real code i have HTTP request and text insert after it.
        Not to be picky about it, but don't even use sleep to demonstrate a slowdown..... in a GUI it completely gives you bad behavior.

        Here is one way to make a non-blocking delay without sleep

        #!/usr/bin/perl use warnings; use strict; use Tk; $|=1; my $count = 0; my $loop = 0; my $mw = new MainWindow(); $mw->Label( -textvariable => \$count )->pack; $mw->Button( -text => 'Start', -command => \&long_job ) ->pack( -side => 'left' ); $mw->Button( -text => 'Stop', -command => sub { $loop = 0; print "$cou +nt\n"; } ) ->pack( -side => 'left' ); MainLoop(); sub long_job { my $var; my $timer = $mw->repeat(1000, sub{$var++} ); $loop =1; while($loop){ $loop++; $mw->waitVariable(\$var); $count++; } DoOneEvent(); }

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://857650]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-07-31 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (245 votes), past polls