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


In reply to Re: Opening another Perl script in Current Window using the perl Tk module by kcott
in thread Opening another Perl script in Current Window using the perl Tk module by TheNewGuyShubh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.