http://www.perlmonks.org?node_id=999303


in reply to Re^3: Error Not a tk object / Problems with multiple files
in thread Error Not a tk object / Problems with multiple files

Oh yes . it is Perl/Tk.
Thank you so much for the prompt reply..

Replies are listed 'Best First'.
Re^5: Error Not a tk object / Problems with multiple files
by zentara (Archbishop) on Oct 16, 2012 at 12:14 UTC
    Can you suggest me how to do that ??

    Sure, just substitute new Toplevel for new MainWindow, and remove all extra MainLoop declarations. A new MainWindow and MainLoop go in pairs, and you only need 1 pair.

    There is 1 problem with raising alot of Toplevel windows, and that is sometimes( depending on how your script is written) they can cause an accumulation of memory if repeatedly called. Here is a general purpose example:

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->title( "MainWindow" ); my $spawn_button = $mw->Button( -text => "Toplevel", -command => \&do_Toplevel )->pack(); my $change_button = $mw->Button( -text => "Toplevel repacked", -command => \&do_Toplevel_repack )->pack(); ######### make a top level withdrawn ################## # make $tl global so it's memory space is reused my $tl = $mw->Toplevel(); $tl->protocol('WM_DELETE_WINDOW' => sub { print "do nothing here\n"; #prevents destruction of $tl #by WM control }); $tl->geometry('300x300-1-1'); $tl->title( "Toplevel" ); $tl->Button( -text => "Close", -command => sub { $tl->withdraw; $spawn_button->configure(-state=>'normal'); $change_button->configure(-state=>'normal'); })->pack(); $tl->withdraw; MainLoop; sub do_Toplevel { $spawn_button->configure(-state=>'disabled'); $change_button->configure(-state=>'disabled'); $tl->deiconify(); $tl->raise(); } sub do_Toplevel_repack { $spawn_button->configure(-state=>'disabled'); $change_button->configure(-state=>'disabled'); #clean out top level my @w = $tl->packSlaves; foreach (@w) { $_->packForget; } $tl->title( "Toplevel repack" ); $tl->geometry('300x500-1-1'); $tl->Button( -text => "Close1", -command => sub { $tl->withdraw; $spawn_button->configure(-state=>'normal'); $change_button->configure(-state=>'normal'); })->pack(); my $text = $tl->Scrolled('Text')->pack(); for (1..100){ $text->insert('end', "$_\n"); $text->see('end'); } #add whatever widgets you want here # Entries, etc $tl->Button( -text => "Add button to mainwindow", -command => sub { $mw->Button(-text=>'new Button')->pack(-side =>'bottom'); })->pack(); $tl->deiconify(); $tl->raise(); }
    The first thing to try is substitute all your secondary MainWindow/MainLoop declarations with new Toplevel and see how it goes. Watch your memory, and if it seems to gain with repeated use, then use the techniques in the above code to reuse your Toplevels by withdrawing them, and repacking them, for reuse.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thank you so much..!! you've been of great help..