销毁 Tk 中的小部件不起作用

Destroy Widget in Tk not working

我有一个 Pertl Tk 代码,我想关闭主 window 并打开另一个,但是在第一个 window 再次关闭后,第二个 window 首先打开 window也出现了。

代码

 use strict;
 use Tk;

 my $mw;

 #Calling the welcome_window sub
 welcome_window();

 sub welcome_window{

    #GUI Building Area
    $mw = new MainWindow; 

    my $frame_header = $mw->Frame();
    my $header = $frame_header -> Label(-text=>"Molex Automation Tool");
    $frame_header -> grid(-row=>1,-column=>1);
    $header -> grid(-row=>1,-column=>1);

    my $region_selected = qw/AME APN APS/;

    my $frame_sub_header = $mw->Frame();
    my $sub_header = $frame_sub_header -> Label(-text=>"Region Selection");
    $frame_sub_header -> grid(-row=>2,-column=>1);
    $sub_header -> grid(-row=>2,-column=>1);


    my $frame_region = $mw->Frame();
    my $label_region = $frame_region -> Label(-text=>"Region:");
    my $region_options = $frame_region->Optionmenu(
        -textvariable => $region_selected,
        -options  => [@regions],
    );
    $frame_region -> grid(-row=>3,-column=>1);
    $label_region -> grid(-row=>3,-column=>1);
    $region_options -> grid(-row=>3,-column=>2);

    my $frame_submit = $mw->Frame();
    my $submit_button = $frame_submit->Button(-text    => 'Go!',
                  -command => \&outside,
            );
    $frame_submit -> grid(-row=>4,-column=>1,-columnspan=>2);
    $submit_button -> grid(-row=>4,-column=>1,-columnspan=>2);

    MainLoop;
}

#This sub is just made to close the main window created in Welcome_Wiondow() sub and call the second_window()

sub outside{

    $mw -> destroy;
    sleep(5);
    second_window();
}


sub second_window{

    my $mw2 = new MainWindow;
    my $frame_header2 = $mw2->Frame();
    my $header2 = $frame_header2 -> Label(-text=>"Molex Automation Tool");
    $frame_header2 -> grid(-row=>1,-column=>1);
    $header2 -> grid(-row=>1,-column=>1);

    my $frame_sub_header2 = $mw2->Frame();
    my $sub_header2 = $frame_sub_header2 -> Label(-text=>"Tasks Region Wise");
    $frame_sub_header2 -> grid(-row=>2,-column=>1);
    $sub_header2 -> grid(-row=>2,-column=>1);

    MainLoop;
}

我减少了代码,只放了相关的行。现在请让我知道为什么我不能在子 outside() 中杀死在子 welcome_window() 中打开的主 window。目前它所做的是在睡眠命令期间关闭主 windows 但是一旦我打开 [=19= 的主 windows,[=18= 的 windows ]也再次出现

Got the above code working now, there was some issue in the logic which was calling the welcome_window again. Thank you all for your help.

您不能拥有多个主窗口。为初始创建一个 Toplevel window,使用 withdraw 隐藏真正的主要 window 在开始时,使其重新出现 deiconify:

my $mw = MainWindow->new;
my $tl = $mw->Toplevel;
$tl->protocol(WM_DELETE_WINDOW => sub {
    $mw->deiconify;
    $tl->DESTROY;
});
$mw->withdraw;
MainLoop();