2017-05-25 129 views
1

我對Perl TK沒有什麼問題。我正在使用Active State Perl(v5.16.3)。我創建了一個窗口,其中兩個菜單使用下面的代碼。當我按兩次在我的子菜單(分析 - >輸入分析)。如果我做單擊然後它工作正常。然後所有的小部件都不會被破壞我可以知道我做錯了嗎?下面是我的代碼: -Perl TK窗口部件銷燬

use warnings; 
use strict; 
use 5.010; 
use Tk; 

my ($Lab1, $Lab2, $Lab3, $Lab4, $Lab5, $Lab6); 
my ($Entry1, $Entry2, $Entry3, $Entry4, $Entry5, $Entry6); 
my ($Button1, $Button2, $Button3, $Button4); 
my ($path_Button1, $path_Button2, $path_Button3, $path_Button4, $path_Button5, $path_Button6); 
my $dirname1; 
my $dirname2; 
my $dirname3; 
my $path_Button_input; 
my $path_Button_output; 

#Now create window and Layout:- 
my $analysis_report = MainWindow->new(-width => '640', -height => '480', -relief => 'flat', -title => 'I/O Analysis',); 
$analysis_report->geometry("740x580"); 

my $menubar = $analysis_report -> Menu(); 
$analysis_report -> configure(-menu => $menubar); 

#The Main Buttons 
my $analysis = $menubar->cascade(-label => "Analysis", -tearoff => 0); 
## Menu option ## 
$analysis->command(
    -label  => 'Input Analysis', 
    #-accelerator => 'Ctrl-N', 
    -underline => 0, 
    -command  => \&path_Button_I, 
); 
$analysis->separator; 
$analysis->command(
    -label  => 'Output Analysis', 
    #-accelerator => 'Ctrl-D', 
    -underline => 0, 
    -command  => \&path_Button_O, 
); 

my $clear = $menubar->cascade(-label => "Clear All", -tearoff => 0); 
$clear->command(
    -label  => 'Clear', 
    #-accelerator => 'Ctrl-N', 
    -underline => 0, 
    -command  => \&clear, 
); 

sub path_Button_I 
{ 
    $Lab1 = $analysis_report->Label(-pady => '10', -padx => '10', -relief => 'flat', -state => 'normal', -justify => 'center', -text => 'XML input file: ', -font => "Courier 10 bold")->place(-x => 5, -y => 90); 
    $Entry1 = $analysis_report->Entry(-width => '50', -relief => 'sunken', -state => 'normal', -justify => 'left', -textvariable=>\$dirname1,)->place(-x => 170, -y => 100); 
    $path_Button1 = $analysis_report->Button(-pady => '0', -padx => '0', -relief => 'raised', -state => 'normal', -justify => 'center', -text => '...', -font => "Courier 11 bold", -command => \&path_open1)->place(-x => 500, -y => 95); 
    $Lab2 = $analysis_report->Label(-pady => '10', -padx => '10', -relief => 'flat', -state => 'normal', -justify => 'center', -text => 'Enter XSLT path: ', -font => "Courier 10 bold")->place(-x => 5, -y => 160); 
    $Entry2 = $analysis_report->Entry(-width => '50', -relief => 'sunken', -state => 'normal', -justify => 'left', -textvariable=>\$dirname2,)->place(-x => 170, -y => 170); 
    $path_Button2 = $analysis_report->Button(-pady => '0', -padx => '0', -relief => 'raised', -state => 'normal', -justify => 'center', -text => '...', -font => "Courier 11 bold", -command => \&path_open2)->place(-x => 500, -y => 165); 
    $Lab3 = $analysis_report->Label(-pady => '10', -padx => '10', -relief => 'flat', -state => 'normal', -justify => 'center', -text => 'Enter Log path:', -font => "Courier 10 bold")->place(-x => 5, -y => 230); 
    $Entry3 = $analysis_report->Entry(-width => '50', -relief => 'sunken', -state => 'normal', -justify => 'left', -textvariable=>\$dirname3,)->place(-x => 170, -y => 240); 
    $path_Button3 = $analysis_report->Button(-pady => '0', -padx => '0', -relief => 'raised', -state => 'normal', -justify => 'center', -text => '...', -font => "Courier 11 bold", -command => \&path_open3)->place(-x => 500, -y => 235); 
    $Button1 = $analysis_report->Button(-pady => '10', -padx => '10', -relief => 'raised', -state => 'normal', -justify => 'center', -text => 'Transformation', -font => "Courier 11 bold", -command => \&trans_sub)->place(-x => 150, -y => 300); 
    $Button2 = $analysis_report->Button(-pady => '10', -padx => '10', -relief => 'raised', -state => 'normal', -justify => 'center', -text => 'Clear Logs', -font => "Courier 11 bold", -command => \&clear_log)->place(-x => 350, -y => 300); 
} 

sub path_Button_O 
{ 
    $Lab4 = $analysis_report->Label(-pady => '10', -padx => '10', -relief => 'flat', -state => 'normal', -justify => 'center', -text => 'Out XML input file: ', -font => "Courier 10 bold")->place(-x => 5, -y => 90); 
    $Entry4 = $analysis_report->Entry(-width => '50', -relief => 'sunken', -state => 'normal', -justify => 'left', -textvariable=>\$dirname1,)->place(-x => 170, -y => 100); 
    $path_Button4 = $analysis_report->Button(-pady => '0', -padx => '0', -relief => 'raised', -state => 'normal', -justify => 'center', -text => 'Out ...', -font => "Courier 11 bold", -command => \&path_open1)->place(-x => 500, -y => 95); 
    $Lab5 = $analysis_report->Label(-pady => '10', -padx => '10', -relief => 'flat', -state => 'normal', -justify => 'center', -text => 'Out Enter XSLT path: ', -font => "Courier 10 bold")->place(-x => 5, -y => 160); 
    $Entry5 = $analysis_report->Entry(-width => '50', -relief => 'sunken', -state => 'normal', -justify => 'left', -textvariable=>\$dirname2,)->place(-x => 170, -y => 170); 
    $path_Button5 = $analysis_report->Button(-pady => '0', -padx => '0', -relief => 'raised', -state => 'normal', -justify => 'center', -text => 'Out ...', -font => "Courier 11 bold", -command => \&path_open2)->place(-x => 500, -y => 165); 
    $Lab6 = $analysis_report->Label(-pady => '10', -padx => '10', -relief => 'flat', -state => 'normal', -justify => 'center', -text => 'Out Enter Log path:', -font => "Courier 10 bold")->place(-x => 5, -y => 230); 
    $Entry6 = $analysis_report->Entry(-width => '50', -relief => 'sunken', -state => 'normal', -justify => 'left', -textvariable=>\$dirname3,)->place(-x => 170, -y => 240); 
    $path_Button6 = $analysis_report->Button(-pady => '0', -padx => '0', -relief => 'raised', -state => 'normal', -justify => 'center', -text => 'Out ...', -font => "Courier 11 bold", -command => \&path_open3)->place(-x => 500, -y => 235); 
    $Button3 = $analysis_report->Button(-pady => '10', -padx => '10', -relief => 'raised', -state => 'normal', -justify => 'center', -text => 'Out Transformation', -font => "Courier 11 bold", -command => \&trans_sub)->place(-x => 150, -y => 300); 
    $Button4 = $analysis_report->Button(-pady => '10', -padx => '10', -relief => 'raised', -state => 'normal', -justify => 'center', -text => 'Out Clear Logs', -font => "Courier 11 bold", -command => \&clear_log)->place(-x => 350, -y => 300); 
} 

sub clear 
{ 
    if (Exists($Lab1)) 
    { 
     $Lab1->destroy(); 
     $Lab2->destroy(); 
     $Lab3->destroy(); 
     $Entry1->destroy(); 
     $Entry2->destroy(); 
     $Entry3->destroy(); 
     $path_Button1->destroy(); 
     $path_Button2->destroy(); 
     $path_Button3->destroy(); 
     $Button1->destroy(); 
     $Button2->destroy(); 
    } 
    if (Exists($Lab4)) 
    { 
     $Lab4->destroy(); 
     $Lab5->destroy(); 
     $Lab6->destroy(); 
     $Entry4->destroy(); 
     $Entry5->destroy(); 
     $Entry6->destroy(); 
     $path_Button4->destroy(); 
     $path_Button5->destroy(); 
     $path_Button6->destroy(); 
     $Button3->destroy(); 
     $Button4->destroy(); 
    } 
} 
$analysis_report->MainLoop(); 
+1

好像Tk的保持在窗口小部件的多個副本。但是你只能摧毀其中的一套。所以他們似乎並沒有消失。例如,在'path_Button_I'的開頭,你可以把'return if Exists($ Lab1)' –

+1

謝謝兄弟。你可以嘗試解決這個問題,而不是重新創建這些小部件。這將工作。 –

回答

1

對於每次運行Input Analysis菜單項時,都會創建一套新的小部件,準確地覆蓋在現有的(因爲你使用的地方几何管理器)。

但是,當你運行clear,你只能破壞一組小部件(最後的)的。所以之前的小部件仍然存在,因此clear似乎沒有任何影響。

您可以通過不重新創建控件解決問題,如果他們已經存在,例如:

sub path_Button_I 
{ 
    return if Exists($Lab1); 
    ... 
} 

sub path_Button_O 
{ 
    return if Exists($Lab4); 
    ... 
}