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.


Perl is environmentally friendly - it saves trees

In reply to Re: Unable to delete a specified toplevel by GrandFather
in thread Unable to delete a specified toplevel by kranthi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.