in reply to Multiple Windows in Perl/Tk

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

Replies are listed 'Best First'.
Re: Re: Multiple Windows in Perl/Tk
by jdtoronto (Prior) on Jul 17, 2003 at 03:06 UTC
    Bob, I set about looking at Mastering Perl/Tk more intensely after your reply. What I needed to read was the section commencing on page 238 which talks about Methods on the Toplevel widget.

    Thanks for the clues.

    John