Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi PhysiciSteve,

It may help us more if you show what you've tried.

Having said that, I'll give you a very simple example that may do more-or-less what you need. It starts a worker thread which calculates the localtime every minute, and passes that to the main thread through the shared variable $ltime. It's important to keep the Tk code separate from the thread code (because Tk is not thread-safe), which is why the textvariable '$lbl_label' was kept separate from $ltime, and only assigned from $ltime from within the Tk idle loop update_gui().

You can, of course, change sleep 60; to a smaller interval to see it update more often:

#!/usr/bin/perl -w # Libraries use warnings; use strict; use threads; use threads::shared; use Tk; use Tk::Font; # Shared variables my $ltime : shared = 0; # Globals my $lbl_label = "Started"; # Main Program my $thread = threads->create(\&worker_thread); $thread->detach; create_gui(); # Subroutines sub worker_thread { while (1) { $ltime = localtime(time()); print "Debug> In worker_thread(): Updated localtime to '$ltim +e'\n"; sleep 60; } } sub create_gui { my $mw = new MainWindow(-title => 'Thread example'); my $fr = $mw->Frame->pack(-expand => 1, -fill => 'both'); my $fnt = $mw->Font(-family => 'arial', -size => '12'); my $lbl = $fr->Label(-textvar => \$lbl_label, -background => '#ffe +fb5'); my $btn = $fr->Button(-text => 'Exit (ESC)', -background => 'cyan' +); $btn->configure(-command => sub { exit }, -font => $fnt); $lbl->configure(-font => $fnt); $lbl->pack(-side => 'left'); $btn->pack(-side => 'right'); $mw->bind("<Escape>" => sub { $btn->invoke }); $mw->repeat(1000 => \&update_gui); MainLoop; } sub update_gui { printf "Debug> In update_gui(): %s\n", time(); $lbl_label = $ltime; }

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

In reply to Re: Perl/Tk threading and/or cron job? by liverpole
in thread Perl/Tk threading and/or cron job? by PhysiciSteve

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found