Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: Dynamically Filling Text Box from Perl Tk

by zentara (Archbishop)
on Nov 07, 2011 at 22:46 UTC ( [id://936604]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Dynamically Filling Text Box from Perl Tk
in thread Dynamically Filling Text Box from Perl Tk

I still can't run the code, because of Excel, and an incomplete example, but from what I can gather from your problem, is you think tying STDOUT to the text widget is the only way to print to it. As choroba mentions below, the normal way to print to a text widget, is to do inserts.

Also depending on what the module product_search does, it MAY render the Tk GUI's eventloop screwed up. You might want to make product_search operate in a separate thread, or put it in a standalone script, or run it thru IPC::Open3 or a piped open and collect the results back thru Tk's fileevent. Then you can decide when you want to write these results to the text widget, with an $txt->insert( 'end', $result );

Here is a simple example showing the concepts I described.

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(-background => 'gray50'); my $text = $mw->Scrolled('Text', -bg=>'white' )->pack(); my $pid; my $startb = $mw->Button( -text => 'Start', -command=> \&work, )->pack(); my $count = 0; my $label = $mw->Label(-textvariable=>\$count)->pack(); my $testtimer = $mw->repeat(500, sub { $count++} ); my $stopb = $mw->Button( -text => 'Exit', -command=>sub{ kill 9,$pid; exit; }, )->pack(); MainLoop; ##################################### sub work{ $startb->configure(-state=>'disabled'); use Fcntl; + my $flags; + # 3 second delay between outputs $pid = open (my $fh, "top -b -d 3 |" ) or warn "$!\n"; fcntl($fh, F_SETFL, O_NONBLOCK) || die "$!\n"; # Set the non-block f +lags my $repeater; $repeater = $mw->repeat(10, sub { if(my $bytes = sysread( $fh, my $buf, 1024)){; $text->insert('end',$buf); $text->see('end'); } } ); }

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

Replies are listed 'Best First'.
Re^6: Dynamically Filling Text Box from Perl Tk
by amdme127 (Novice) on Nov 08, 2011 at 15:29 UTC

    I actually just figured it out. By passing the $tx variable to the module and using:

    $tx->update;

    I use that after each print statement in the loop, at this time I see no loss in performance, if there is, it is insignificant with the data I am working with

    Thanks for all your help guys, if I end up having problems with this, I will consult the alternatives in this thread (they seem a little more complex, I just started using Perl Tk, so I got a lot to learn)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-20 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found