Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Executing functions in Perl::Tk

by liverpole (Monsignor)
on Feb 24, 2010 at 00:49 UTC ( #824969=note: print w/replies, xml ) Need Help??

in reply to Executing functions in Perl::Tk

Yes, there are a couple of ways.

The really simple approach is just to call the method update() on the main window object at appropriate times during the subroutine execution:

# Assuming my $mw = MainWindow(...) sub button_press { # Do some work $mw->update; # Do some more work $mw->update; # etc...

The more complex (but very powerful) approach is to use threads, as long as you're very careful not to do anything Tk-related within the thread.  (That's because Tk is not "thread-safe").

Here's an example of a way you could use threads, especially if there are parts of the worker subroutine that have long delays during which you don't have the luxury of using $mw->update:

#!/usr/bin/perl -w # # Libraries use strict; use warnings; use threads; use Tk; use Tk::Font; use Tk::ROText; # Main Program my $mw = new MainWindow(-title => 'Tk thread example'); my $fnt = $mw->Font(-family => 'tahoma', -size => '24'); my $btn = $mw->Button(-text => 'Press Me', -font => $fnt)->pack(-fill +=> 'x'); $btn->configure(-bg => '#ff7f3f', -command => \&button_press); my $txt = $mw->ROText(-bg => 'white')->pack(-fill => 'both'); $mw->bind('<Escape>' => sub { exit }); $mw->repeat(1000 => \&gui_loop); MainLoop; # Subroutines sub gui_loop { $txt->insert('end', sprintf "Time is %s\n", scalar localtime time( +)); $txt->see('end'); } sub button_press { my $sthread = threads->new(sub { worker_thread() }); $sthread->detach(); # Here we "return immediately" :) return; } sub worker_thread { my $tid = threads->tid; # But DON'T do this ... !!! # my $thread_mw = new MainWindow(); for (my $i = 0; $i < 1024; $i++) { printf "Thread %s: Doing more work ...\n", $tid; sleep 1; } }

Please note that once you're in the worker thread, you must refrain from the urge to use Tk.  If you want to pass data back to the main program, you should look into shared data between threads.

If you try putting back in the code where it says "But DON'T do this", you'll get an error message like:

Thread 1: Doing more work ... Attempt to free non-existent shared string '_ErrorInfo_', Perl interpr +eter: 0x23 3f3c at C:/Perl/site/lib/ line 423.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://824969]
[stonecolddevin]: erix thanks mate
[stonecolddevin]: also lol @ "I stomped a crap"
[stonecolddevin]: Disciplus i was just amazed he's returned after the thorough stomp that BrowserUk and several other monks gave him for spreading crap info
[stonecolddevin]: i will admit i find the responses to his threads incredibly entertaining
[stonecolddevin]: erix what is this wizardy? there's no sound for me :-(
[stevieb]: he comes and goes every few years.
[erix]: stonecolddevin: oh? Don't know, it works for me
[Sinistral]: stonecolddevin / stevieb - would 'he' in this context (looked at last 50 lines and didn't see discussion) be a certain solar timepiece user?
[stonecolddevin]: Sinistral yes indeed

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-06-22 20:48 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (529 votes). Check out past polls.