我对Perl TK没什么问题。我正在使用Active State Perl(v5.16.3)。我创建了一个窗口,其中两个菜单使用下面的代码。当我在子菜单中按两次时(分析 - >输入分析)。如果我单击,那么它工作正常。然后所有小部件都不会被破坏。我可以知道我哪里做错了。以下是我的代码: -
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();
答案 0 :(得分:1)
每次运行Input Analysis
菜单项时,都会创建一组新的小部件并完全覆盖现有的小部件(因为您使用了地点几何管理器)。
但是当你运行clear
时,你只会破坏一组小部件(最后一组)。所以之前的小部件仍然存在,因此clear
似乎没有任何影响。
如果小部件已经存在,您可以通过不重新创建小部件来解决问题,例如:
sub path_Button_I
{
return if Exists($Lab1);
...
}
sub path_Button_O
{
return if Exists($Lab4);
...
}