tags:

views:

43

answers:

1

In my Perl/Tk script I have opened two windows. After a specific button click I want to close one of them. How can I do that? Here's what I have so far:

$main = new MainWindow;
$sidebar = $main->Frame(-relief => "raised", 
                        -borderwidth => 2)
                ->pack (-side=>"left" ,
                        -anchor => "nw", 
                        -fill   => "y");
$Button1 = $sidebar -> Button (-text=>"Open\nNetlist", 
                               -command=>  \&GUI_OPEN_NETLIST) 
                    ->pack(-fill=>"x");
MainLoop;

sub GUI_OPEN_NETLIST
{
    $component_dialog = new MainWindow;
    $Button = $component_dialog -> Button (-text=>"Open\nNetlist", 
                                           -command=>  **close new window**) 
                                ->pack(-fill=>"x"); 
    MainLoop;
}
+1  A: 

The simplist way is to call $component_dialog->destroy in the buttons -command callback. This has the disadvantage that if you want to redisplay the window later you have to recreate it. The withdraw method will hide the window without destroying it so you can redisplay it later if you need to. This will save you some time when the button is pressed. The classes Dialog and DialogBox do this for you automatically when one of their buttons is pressed. If you need a window that behaves like a traditional dialog they can a much simpler option that building your own.

Also except in unusual cases you shouldn't need more than one call to MainLoop. When your callback GUI_OPEN_NETLIST returns the MainLoop will resume, explicitly calling MainLoop will likely lead to odd bugs later.

I think this is close to what your looking for, I haven't tested it though.

use strict;
use warnings;

my $main = new MainWindow;
my $sidebar = $main->Frame(-relief => "raised", 
                        -borderwidth => 2)
                ->pack (-side=>"left" ,
                        -anchor => "nw", 
                        -fill   => "y");
my $Button1 = $sidebar -> Button (-text=>"Open\nNetlist", 
                               -command=>  \&GUI_OPEN_NETLIST) 
                    ->pack(-fill=>"x");
my $component_dialog = $main->Dialog( -buttons => [ 'Close' ], );

MainLoop;

sub GUI_OPEN_NETLIST
{
    $component_dialog->Show();
}

If you don't want a dialog you should consider if you want to create a second MainWindow or create a Toplevel window dependant on your existing MainWindow. A Toplevel will close automaticaly when it's MainWindow is closed, a second MainWindow will stay open after the other MainWindow is closed.

Ven'Tatsu