Reputation: 138
I have little problem with Perl TK. I am using Active State Perl (v5.16.3). I was create one window in which two menu using below code. When i press two time in my sub menu (Analysis->Input Analysis).If i do single click then it work fine. Then all widget are not destroy. May i know where i do wrong. Below is my code:-
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();
Upvotes: 1
Views: 788
Reputation: 40718
For each time you run the Input Analysis
menu item, a new set of widgets is created and overlayed the existing ones exactly (since you use the place geometry manager).
But when you run clear
, you only destroy one set of the widgets (the last ones). So the previous widgets are still there and hence clear
does not seem to have any effect at all.
You can solve the issue by not recreating the widgets if they already exist, for example:
sub path_Button_I
{
return if Exists($Lab1);
...
}
sub path_Button_O
{
return if Exists($Lab4);
...
}
Upvotes: 1