There are a slew of problems with your original script. Using strictures and not using our is part of the problem, but the issues go much deeper than that.
The following sample derived from your original code fixes the main problems I found:
#!/usr/local/bin/perl use strict; use Tk; use Tk::BrowseEntry; my $obj = bless {mw => new MainWindow}; $obj->{mw}->title("Main Window"); $obj->{frame1} = $obj->{mw}->Frame()->pack(-side => 'top' ); my $Win_value; #List for Browse Entry my @List = ("First window", "Second Window", "Third Window", "Last Win +dow"); my $browseentry = $obj->{frame1}->BrowseEntry ( -label => "Select Window:", -listheight=> 4, -variable => \$Win_value, -browsecmd => [\&Create_Window, $obj], ) ->pack(-side =>'left', -ipady => 5, -pady=>15); $browseentry->insert('end', @List); my $frame2 = $obj->{mw}->Frame()->pack (-side => 'bottom'); my $lbl1 = $frame2->Button (-text => 'Close', -command =>sub {$obj->{m +w}->destroy()}) ->pack (-pady =>10); MainLoop; sub Create_Window { my $self = shift; my $myId = $_[1]; my $caption = join '', $_[1],"_Details"; if (exists $self->{windows}{$myId} and Exists ($self->{windows}{$m +yId}{win})) { $self->{windows}{$myId}{win}->deiconify (); $self->{windows}{$myId}{win}->raise (); return; } my $info = $self->{windows}{$myId} = {}; $info->{win} = $self->{mw}->Toplevel(); $info->{win}->title("$caption"); $info->{win}->minsize(qw(800 500)); $info->{win}->focus(); $info->{frame} = $info->{win}->Frame()->pack(-side => 'bottom' ); #opens the save file dialog wrt the selected toplevel. $info->{frame}->Button( -text => 'Save', -command =>sub {SaveFile($info->{win})} )->pack(-side=>'left'); #closes the selected window $info->{frame}->Button ( -text => 'Close', -command => [\&closeChild, $self, $myId], )->pack(-side=>'left'); } sub closeChild { my ($self, $winId) = @_; $self->{windows}{$winId}{win}->destroy (); delete $self->{windows}{$winId}; }
Note the use of very light weight object orientated programming techniques to avoid accessing global variables from within subs to get at state information.
Also note the -command => [\&closeChild, $self, $myId] technique to pass parameters to a callback routine.
The sample allows windows to be opened and closed in any order. It allows closed windows to be re-created and shifts focus from the main window to an existing created window (using the list box). The created windows may be closed using either the close button in the window or the close box in the window's title bar. Using the title bar close doesn't get the closeChild callback called however.
In reply to Re: Unable to delete a specified toplevel
by GrandFather
in thread Unable to delete a specified toplevel
by kranthi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |