Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Opening another Perl script in Current Window using the perl Tk module

by TheNewGuyShubh (Initiate)
on Sep 12, 2016 at 14:38 UTC ( [id://1171590]=perlquestion: print w/replies, xml ) Need Help??

TheNewGuyShubh has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Experts

I am trying to opening another perl file(I mean run the perl script file which is another Tk perl Gui window) in my current perl tk Window. I have created a menu and and trying to access the file using open but this only reads the files and I cannot run the perl code in the file. I am new to Perl so your help is really appreciated! TIA! TheNewGuyShubh

my $mw = MainWindow->new(); $mw->title("Test"); $mw->geometry('300x300'); my $menubar1 = $mw->Frame(qw/-relief raised -borderwidth 2/); $menubar1->pack(qw/-fill x/); my $file1 = $menubar1->Menubutton(qw/-text File -underline 0/,); # -menuitems => file_menuitems); my $edit1 = $menubar1->Menubutton(qw/-text Edit -underline 0/,); #-menuitems => edit_menuitems); my $help1 = $menubar1->Menubutton(qw/-text Help -underline 0/, ); #-menuitems => help_menuitems); $file1->pack(qw/-side left/); $edit1->pack(qw/-side left/); $help1->pack(qw/-side right/); my $new = $file1->cascade( -label => 'Open', -accelerator => 'Ctrl-n', -underline => 0, ); $new->command( -label => 'CRA', -accelerator => 'Ctrl-o', -underline => 1, -command => open my $fh,'<','C:/User/MyUser/Desktop/CRA.pl', );
  • Comment on Opening another Perl script in Current Window using the perl Tk module
  • Download Code

Replies are listed 'Best First'.
Re: Opening another Perl script in Current Window using the perl Tk module
by ExReg (Priest) on Sep 12, 2016 at 16:46 UTC

    What your code is doing now is opening the other perl file and giving you a file handle, $fh, to do something with the contents of the file, such as read it. I think what you want to do is not read the file, but execute the file. To do that, you need to execute the file, either through a system call or a backtick call, i.e.

    system( 'perl C:/User/MyUser/Desktop/CRA.pl' ); or `perl C:/User/MyUser/Desktop/CRA.pl`;

    This will run the other perl file in another process.

Re: Opening another Perl script in Current Window using the perl Tk module
by kcott (Archbishop) on Sep 14, 2016 at 12:31 UTC

    G'day TheNewGuyShubh,

    Welcome to the Monastery.

    "I am trying to open ... another Tk perl Gui window ... in my current perl tk Window."

    Below, I've referred to the current window as the master and the window (opened in the master) as the slave.

    This operation is called embedding. You create the master with either a Tk::Frame, a Tk::Toplevel or a Tk::MainWindow, which has its '-container' option set to a TRUE value. This container widget is where the slave will be embedded: you'll need its ID ($container->id) which is explained in Tk::Widget. You create the slave with a Tk::MainWindow which has its '-use' option set to the container ID.

    Important: All of the code below is intended to demonstrate a technique only! It is very much bare-bones code. To make it production-ready, you'll need to add validation, error-checking and so on.

    First, let's look at an example slave (pm_1171590_tk_embed_slave.pl). Note that this can be run both as a stand-alone and an embedded application.

    #!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow::->new(-use => $ARGV[0] || ''); $mw->Label(-text => 'Slave')->pack; MainLoop;

    Running this as stand-alone without arguments produces a GUI like this:

    +-------+ | Slave | +-------+

    Attempting to run stand-alone with arguments, generates an error like this:

    7fcb5c12a4a0 is not a hash at /.../Tk/MainWindow.pm line 53. Abort trap: 6

    Now let's look at an example master (pm_1171590_tk_embed_master.pl) that may (or may not) have a specified slave.

    #!/usr/bin/env perl use strict; use warnings; use autodie qw{:all}; use Tk; my $mw = MainWindow::->new(); $mw->Label(-text => 'Embedded slave:')->pack; if (length $ARGV[0]) { my $slave_frame = $mw->Frame(-container => 1)->pack; my $container_id = $slave_frame->id; open my $pipe, '-|', "$ARGV[0] $container_id"; MainLoop; close $pipe; } else { my $slave_frame = $mw->Frame()->pack; $slave_frame->Label(-text => 'No slave supplied!')->pack; MainLoop; }

    Running without a specified slave.

    $ pm_1171590_tk_embed_master.pl

    produces a GUI which looks like

    +--------------------+ | Embedded slave: | | No slave supplied! | +--------------------+

    Running with a specified slave.

    $ pm_1171590_tk_embed_master.pl pm_1171590_tk_embed_slave.pl

    produces a GUI which looks like

    +-----------------+ | Embedded slave: | | Slave | +-----------------+

    See also: autodie and open.

    — Ken

Re: Opening another Perl script in Current Window using the perl Tk module
by Anonymous Monk on Sep 14, 2016 at 19:46 UTC

    I am trying to opening another perl file(I mean run the perl script file which is another Tk perl Gui window) in my current perl tk Window.

    :) You should stop trying that and start creating modules

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1171590]
Approved by sweetblood
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-23 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found