Perl TK窗口小部件销毁

Perl TK窗口小部件销毁,perl,tk,activestate,Perl,Tk,Activestate,我对Perl-TK没有什么问题。我使用的是活动状态Perl(v5.16.3)。我创建了一个窗口,其中两个菜单使用下面的代码。当我在我的子菜单(分析->输入分析)中按两次时。如果我单击一下,它工作正常。那么所有的小部件都不会被销毁。我可以知道我哪里做错了吗。下面是我的代码:- use warnings; use strict; use 5.010; use Tk; my ($Lab1, $Lab2, $Lab3, $Lab4, $Lab5, $Lab6); my ($Entry1, $Entry

我对Perl-TK没有什么问题。我使用的是活动状态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();

每次运行
输入分析
菜单项时,都会创建一组新的小部件,并将其完全覆盖到现有的小部件上(因为您使用的是place geometry manager)

但是当您运行
clear
时,您只销毁了一组小部件(最后一组)。因此,前面的小部件仍然存在,因此
clear
似乎没有任何效果

如果小部件已经存在,您可以通过不重新创建它们来解决此问题,例如:

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

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

Tk似乎在窗口中保留了多个小部件的副本。但你只摧毁了一套。所以他们似乎没有消失。如果小部件已经存在,您可以尝试通过不重新创建它们来解决此问题:例如,在
path\u Button\u I
的开头,您可以放置
return if Exists($Lab1)
谢谢兄弟。这会奏效的。