Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run perl console app within Tk window

Status
Not open for further replies.

msingle

MIS
Jul 15, 2002
22
0
0
US
How would I run a console app within a TK window??
Thanks.
 
What exactly do you mean?
An app is an executable. So suppose you have an executable
like myprogram.e . You can bind a mouse click or a button selection or whatever to
sub{system "myprogram.e";}

for example. Is that what you're looking for??

svar
 
I have a perl app that runs from a command line, I would like it to run in a Window (either TK or Win32), if this is possible.

use Tk;
my $mw = new MainWindow('title'=>"Title");
<insert cli perl code here to run inside of MainWindow>

 
Here is an example of a trivial counter and a small Tk app to
call the counter and show it's output in a label. You might use a Scrolled Pane or other widget to contain/display the dos applications output, depending on how much there is.

-counter.pl-
Code:
#!perl
$| = 1;
for (0..3) { 
    sleep(1); 
    print &quot;$_\n&quot;; 
}


-call_counter.pl-
Code:
#!perl
use Tk;
my $mw = MainWindow->new;

# give it a title
$mw->configure(-title=>'Uber Simple Tk App.');

# set it's size and location on the screen
$mw->geometry('300x40+100+100');

# build a button to call the counter app.
my $button = $mw->Button(-text=>'Run Counter', 
             -command=>\&call_counter     )  
        ->pack(-side=>'left');
        
# build an 'Exit' button.
$mw->Button(-text=>'Exit', 
             -command=>sub{exit})  
        ->pack(-side=>'right');
        
# build a label to show the responses from the counter app.
my $window_text;       
my $label = $mw->Label(-textvariable=>\$window_text)
    ->pack(-side=>'left');

MainLoop;
#-------------------------------------------

sub call_counter {
    # disable the counter button.
    $button->configure(-state=>'disabled');
    
    # reset label text to nada
    $window_text = '';
    
    # call counter.pl and display results as they are returned 
    # through the pipe.
    open(CMD,&quot;counter.pl |&quot;) or die &quot;Failed to open trivial.pl, $!\n&quot;;
    while (<CMD>) {
        $window_text .= $_ ;
        chomp($window_text);
        
        # force an update of the label widget to display new text.
        $label->update;
    }
    close CMD;
    $window_text .= &quot;\nDone.&quot;;
    $label->update;
    
    # enable button.
    $button->configure(-state=>'normal');
}

Sorry the wrapping is so ugly 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top