Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: backgrounding a Tk process

by zentara (Archbishop)
on Oct 09, 2012 at 14:19 UTC ( #998011=note: print w/ replies, xml ) Need Help??


in reply to backgrounding a Tk process

What I would like to do is be able to run the script, output to Tk and then exit the script, leaving the Tk widget up and running.

The only way to do that would be to fork off the Tk portion, because if you exit the script you would mess up the Tk event loop. Here is a basic example.

#!/usr/bin/perl use warnings; use strict; use Tk; my $data = 'whatever'; # do whatever here to print to STDOUT or xterm # or # to display the Tk if (fork == 0) { my $top = new MainWindow; $top->geometry('200x200+100+100'); $top->Label( -text=> $data )->pack(); $top->Button( -text => 'Exit', -command => sub { exit })->pack; MainLoop; CORE::exit(); } # always do your forks first, before creating Tk in the main script # my $mw = new MainWindow; # MainLoop; # not needed in this example # non Tk script t can continue here for (1.. 3){ print "$_\n"; sleep 1; } exit; #If you need start a new process from one of the child processes, then #you have to establish some kind of IPC (e.g. pipes) between the child #and parent. #I the launched task running another Perl/Tk module or function or a #complete new program? In the latter case, you should just use fork an +d #exit/system and do not forget to use CORE::exit instead of exit in th +e #child process (according to the FAQ). Perl/Tk forks are somewhat more #difficult. You have to make sure that you fork off the process which #does not have a created a MainWindow itself. This will work fine:

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


Comment on Re: backgrounding a Tk process
Download Code
Re^2: backgrounding a Tk process
by mpersico (Beadle) on Oct 10, 2012 at 13:24 UTC
    Ah. I've been building the widget in main code and just forking off the MainLoop call. I'll try it your way. Can you explain why you have two MainLoop calls?
      And my OTHER STUPID MISTAKE was trying to debug in ptkdb, the TK debugger. It can be done but you have to set stuff up to handle the debugger mainloop AND the program mainloop and I completely forgot about that.
      Can you explain why you have two MainLoop calls?

      Oops, it was a typo, as I tried to setup the sample script. In your case, since the main script was not a Tk app itself, it wasn't needed. The important line in the example is CORE::exit, as explained in the Caveat to perldoc Tk::exit

      I updated the example a bit, to show that if you are forking from an existing Tk GUI, the forks must be done first. Otherwise Tk just hangs.


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-09-21 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls