I see a couple of problems.

The "while(<$pipe>)" is not good in gui programming. You should look at Tk::fileevent, and try to design using it. It allows you to watch filehandles in a non-blocking manner.

When using Tk and threads, it is best to use Tk only in your main thread, to display what is going on in the worker threads. In the worker thread, don't access the Tk display widgets, rather pass the data back to the main thread thru a Tk timer and shared variables.

#!/usr/bin/perl use strict; use Tk; use threads; use threads::shared; my $data_out:shared = ''; my $data_in:shared = ''; my $thread_die = 0; #create the thread before you start any Tk so the #thread dosn't get any Tk stuff copied in my $wthr = threads->new( \&update_thread )->detach; create_tk_window(); exit; ######################################################### #the non-Tk worker code sub update_thread { print "update_thread called...\n"; while (1) { if($thread_die == 1){return} $data_in = 'thread-processing'.$data_out; sleep 1; } END: } ######################################################### sub create_tk_window { my $mw = MainWindow->new( -background => 'black', -foreground => 'yellow', -title => "Thread Test" ); $mw->geometry("600x400"); $mw->minsize( 400, 400 ); $mw->maxsize( 800, 800 ); my $sent_text = $mw->Scrolled('Text', -height => 10, -width => 60, -background => 'black', -foreground => 'yellow' )->pack( -side => 'bottom', -anchor => 's', -pady => 2 ); my $received_text = $mw->Scrolled( "Text", -height => 10, -width => 60, -background => 'white', -foreground => 'black', )->pack(); my $repeater; $mw->Button(-text=> 'Exit', -command => sub{ $thread_die = 1; $repeater->cancel; $mw->withdraw; kill 9, $$; })->pack; $repeater = $mw->repeat(1000, sub{ $data_out++; $sent_text->insert( 'end', "Sent $data_out\n" ); $sent_text->see('end'); $received_text->insert( 'end', "Received $data_in\n" ); $received_text->see('end'); }); MainLoop; }

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

In reply to Re: Tk + threads by zentara
in thread Tk + threads by knirirr

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.