Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by kcott (Archbishop)
on Sep 14, 2016 at 12:31 UTC ( [id://1171736]=note: print w/replies, xml ) Need Help??


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

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-24 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found