You can't do threads from within Tk.

However, you could spawn one or more threads outside of Tk, and then use Tk only within one of those threads.

Here's a simplistic example, based on your code, where the "parent" thread runs perl/Tk, and the "worker" thread sends messages back to the parent:

#!/usr/local/bin/perl -w use Tk; use threads; use threads::shared; ############# ## Globals ## ############# my $shared_text: shared = ""; # The message sent back to the +parent my $shared_flag: shared = 0; # 0 = no msg waiting, 1 = msg wa +iting ################## ## Main program ## ################## my $pthread = threads->new(\&worker_thread); $pthread->detach(); my $mw = MainWindow->new(); my $text = $mw->Entry(-width => 8)->pack(); $text->insert(0, 'abcde'); my $button = $mw->Button(-text=>'click', -command => \&talk_to_worker) +->pack(); MainLoop; ################# ## Subroutines ## ################# # Parent subroutines sub talk_to_worker{ while (0 == $shared_flag) { print "(parent) Waiting for flag to go to 1\n"; select(undef, undef, undef, 0.5); } my $msg = $shared_text; print "(parent) Got message '$msg'\n"; if ($msg) { $text->delete('0.0', "end"); $text->insert('0.0', $msg); $mw->update(); } print "(parent) Resetting flag to 0\n"; $shared_flag = 0; } # Worker subroutines sub worker_thread { for my $i (0..100) { sleep 3; send_message_to_parent("-$i"); } } sub send_message_to_parent { my ($msg) = @_; # Wait until the $shared_flag is zero again while (1 == $shared_flag) { print "(worker) Waiting for flag to go to 0\n"; select(undef, undef, undef, 0.5); } # Send the message; $shared_text = $msg; # Raise the flag to handshake with the parent $shared_flag = 1; }

Note the use of threads::shared, which lets you pass messages between threads.  Also, the use of a flag $shared_flag, which is set by the child thread (the "worker" thread) to indicate that it has sent a message in $shared_text, and then reset to zero by the parent thread to indicate that the message has been received.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Perl Tk and Threads by liverpole
in thread Perl Tk and Threads by Anonymous Monk

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.