lara26 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, I am trying to write a simple program in Perl/Tk,
This program creates a Main window which contains a Button named New Window
This button opens other Toplevel window Child Window which contains a text box and button
Enter the text in the text box and click on CREATE btn which opens a top level window with the text as tile.
On clicking on CREATE btn should open multiple Toplevel windows with different titles.
If the titles are same the existing window should raise.
This toplevel window contains Refresh and Close Buttons.
Here i am facing the problem. When i click on the Close button the top level window closes.
But when i click on Refresh Btn, other toplevel window will be refreshed instead of the one that is intended.
Please help me in resolving this issue. Here is the code that i am working with:
#!/usr/local/bin/perl use strict; use Tk; my $obj; my $fp; my $info; my $entframe; #Creating the Main Widow my $wind = new MainWindow; $wind->title("Main Window"); my $frame = $wind->Frame()->pack; $frame->Button(-text => 'New Window', -command =>\&newwin) ->pack(-side =>'left', -ipady => 5, -p +ady=>15); MainLoop; sub newwin { if(! Exists($obj->{mw})) { $obj = bless {mw => $wind->Toplevel()}; $obj->{mw}->title("Child Window"); $obj->{mw}->minsize(qw(200 200)); #The entered text will be the title of the next toplevel windo +w my $frm = $obj->{mw}->Frame()->pack(-side => 'top' ); $frm->Label(-text=>"Enter the Toplevel Name")->pack(); $frm->Entry(-textvariable =>\$fp)->pack(); $obj->{frame1} = $obj->{mw}->Frame()->pack(); $obj->{frame1}->Button(-text => 'CREATE', -command =>[\&Create +_Window, $obj])->pack(-side =>'left', -ipady => 5, -pady=>15); } else { $obj->{mw}->deiconify (); $obj->{mw}->raise (); } } #Creating the Toplevel window sub Create_Window { my $self = $_[0]; my $Obj_Id = $fp ; if (Exists ($self->{windows}{$Obj_Id}{win})) { $self->{windows}{$Obj_Id}{win}->deiconify (); $self->{windows}{$Obj_Id}{win}->raise (); return; } $info = $self->{windows}{$Obj_Id} = {}; $info->{win} = $wind->Toplevel(); my $path = $wind->toplevel; $info->{win}->title($fp); $info->{win}->minsize(qw(200 200)); $info->{win}->focus(); $info->{$entframe} = $info->{win}->Frame()->pack(-side => 'top' ); $info->{$entframe}->Entry()->pack(); $info->{$entframe}->Entry()->pack(); $info->{frame} = $info->{win}->Frame()->pack(-side => 'bottom' ); $info->{frame}->Button(-text => 'Refresh', -command =>[\&refresh, +$self, $Obj_Id])->pack(-side=>'left'); $info->{frame}->Button ( -text => 'Close', -command => [\&closeChi +ld, $self, $Obj_Id] )->pack(-side=>'left'); } #Close the particular Child Window sub closeChild { my ($self, $winId) = @_; my $title = $self->{windows}{$winId}{win}->cget(-title); $self->{windows}{$winId}{win}->destroy (); delete $self->{windows}{$winId}; } #Refresh the particular Child Window sub refresh { my ($self, $winId, $f) = @_; if (Exists($info->{$entframe})) { $info->{$entframe}-> destroy() ; } ############################################################# #Here i am unable to get the reference of the toplevel window $info->{$entframe} = $self->{windows}{$winId}{win}->Frame()->pa +ck(-side => 'top' ); $info->{$entframe}->Entry()->pack(); }

Replies are listed 'Best First'.
Re: Toplevel Window References in Perl Tk
by zentara (Cardinal) on Sep 10, 2008 at 12:04 UTC
    You might get some clues if you add "use warnings;" to your script.

    To be honest, I can't figure out what you are trying to do? :-) I've never seen code like this:

    if(! Exists($obj->{mw})) { $obj = bless {mw => $wind->Toplevel()}; $obj->{mw}->title("Child Window");
    So I'm wondering what you are trying to do?

    Anyways, to get the reference to a Toplevel, just save it somewhere.

    my %toplevels; $toplevel{$count}{'toplevel'} = $wind->Toplevel();
    You seem to be trying to track windows, by setting their Titles, but I may not understand your code. You should save them in hashes or arrays.

    Anyways, add warnings to your script, fix the warnings problem, and straighten out your hashes. Sorry, this code is too convoluted for me to understand.


    I'm not really a human, but I play one on earth Remember How Lucky You Are
      Hi, Thanks for your response. I am able to get the reference of the Toplevel windows but i am unable to get the reference of the widgets that are created in the Toplevel window. {Ex I want to get the reference of the Frame that i have created in Toplevel1} I am unable to get that reference. Any suggestions on the same
        Hi, here is the basic idea. There is some error checking left out, like removing a key from the hash if you destroy a toplevel, but it shows the idea. This is a very straight forward method, but there are others, like looping thru all the pack children of the toplevel, then finding the Frame hash.... but stick with the straight forward until you get more experience.
        #!/usr/bin/perl use warnings; use strict; use Tk; my $count = 0; my %tls; #hash to store the toplevels my $mw = MainWindow->new; $mw->title( "MainWindow" ); my $spawn_button = $mw->Button( -text => "Toplevel", -command => \&do_Toplevel )->pack(); my $report_button = $mw->Button( -text => "Report", -command => \&report )->pack(); MainLoop; sub do_Toplevel { my $num = $count++; $tls{$num}{'tl'} = $mw->Toplevel(); $tls{$num}{'tl'}->protocol('WM_DELETE_WINDOW' => sub { print "do nothing here\n"; #prevents destruction of $tl #by WM control }); $tls{$num}{'tl'}->title( "Toplevel $count" ); $tls{$num}{'frame'}= $tls{$num}{'tl'}->Frame() ->pack(-expand=>1, -fill=>'both'); $tls{$num}{'label'} = $tls{$num}{'frame'}->Label(-text=>$count) ->pack(-expand=>1, -fill=>'x'); $tls{$num}{'tl'}->Button( -text => "Close", -command => sub { $tls{$num}{'tl'}->withdraw; } )->pack; } sub report { foreach my $num (keys %tls){ print "toplevel $num has frame ", $tls{$num}{'frame'},"\n"; } }

        I'm not really a human, but I play one on earth Remember How Lucky You Are