2017-08-11 86 views
1

我有一个Pertl Tk代码,我想关闭主窗口并打开另一个窗口,但第一个窗口在第二个窗口打开后再次关闭时,第一个窗口也会出现。在Tk中销毁Widget不工作

代码

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; 
} 

我减少了代码,并只放了相关线路。现在请让我知道为什么我不能杀掉在sub outside()中的子welcome_window()中打开的主窗口。目前它在睡眠命令期间会关闭主窗口,但只要打开second_windows的主窗口,welcome_window的窗口也会再次出现。

得到了上面的代码现在工作,逻辑 有一些问题,再次调用welcome_window。谢谢大家对你的 帮助。

回答

2

您不能有多个MainWindow。创建初始的一个顶层窗口,使用withdraw隐藏在开始真正的主窗口,使其与deiconify重现:

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

我可以从这个withdaw功能工作主循环外的()?目前我无法做到这一点。 – Mohit

+0

另外我还为每个窗口分开了MainLoop?这是否有区别 – Mohit

+0

我不明白第一个问题。尝试提出一个新问题,而不是评论。你可能有两个MainLoops和两个不同的MainWindow,只要确保在第一个循环结束之前不要定义第二个。 – choroba