Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

backgrounding a Tk process

by mpersico (Beadle)
on Oct 09, 2012 at 13:17 UTC ( #998005=perlquestion: print w/ replies, xml ) Need Help??
mpersico has asked for the wisdom of the Perl Monks concerning the following question:

I have a Perl script that has three output modes - print, print in a spawned xterm and output to a Tk::NoteBook. 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.

I've seen a few things about reconnecting stdout/err and fork execing, but I've had no luck, so I think I must be missing something.

I have resorted in the meantime to moving my script X to X.pl and using the following as X:

usingTk=$(echo $* | grep -i -- '-tk') if [[ "$usingTk" == '' ]]; then perl $(basename $0)/X.pl "$@" else perl $(basename $0)/X.pl "$@" & fi

Surely there is a more correct way to do this. Does anyone know it?

Much obliged - Matt

Comment on backgrounding a Tk process
Download Code
Re: backgrounding a Tk process
by bcarroll (Monk) on Oct 09, 2012 at 13:56 UTC
    You might be able to do this if you make the TK window the default mode and call the other modes from it.

    Default mode = Tk;
    print mode = hide Tk window, spawn a new shell
    xterm mode = hide Tk window, spawn new xterm

    you might want to hide the console window though.

Re: backgrounding a Tk process
by zentara (Archbishop) on Oct 09, 2012 at 14:19 UTC
    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
      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: perlquestion [id://998005]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-09-20 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (155 votes), past polls