In general, you will want to get the book "Mastering Perl/Tk".

When you create the new window, you'll have a 'close' button or menu entry on it. If you have to create this (e.g. there sin't a module that will do this automagically), then you'll need to give this button a callback containing something that will reference the window. In this case, use a closure in the callback to hold the window object for use in destroy():

#!/usr/bin/perl -w use Tk; my $mw = MainWindow->new(-title=>"Demo"); my $HlpBttn = $mw->Button(-text=>"NEW", -command=> sub { make_win(); }); $HlpBttn->pack(); MainLoop; sub make_win { my $win = $mw->Toplevel(-title=>'new window', -height=>10, -width=>50); my $Bttn = $win->Button(-text=>"CLOSE", -command=> sub { close_win($win); } )->pack; } sub close_win { my $thiswin = $_[0]; $thiswin->destroy; }

The callback in each window has it's own instance of $win locked up in the coderef, ( -command => sub { close_win($win) } ), which retains the value $win had at the timew the window was created, so it "knows" which window to destroy when called. This coderef carrying around an instance of a lexical variable from the coderef's enclosing scope is called a "closure".

Update: Created a minimal example, got rid of all the old stuff.

--Bob Niederman, http://bob-n.com

In reply to Re: Multiple Windows in Perl/Tk by bobn
in thread Multiple Windows in Perl/Tk by jdtoronto

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.