Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: Error Not a tk object / Problems with multiple files

by zentara (Archbishop)
on Oct 16, 2012 at 11:59 UTC ( #999298=note: print w/ replies, xml ) Need Help??


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

You said you were using Tcl/Tk, but it looks like a plain Perl/Tk script to me. In any event, your code problem almost certainly stems from having multiple new MainWindow and multiple MainLoop declarations. In Perl/Tk, as in the Highlander, there can only be 1 :-)

The first MainWindow you declare is fine, but all those following ones should be converted to Toplevel windows, which exist under the the single MainLoop of the first MainWindow declared.

Here is a simple usage of Toplevel

#!/usr/bin/perl use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->Label(-text => 'Main Window')->pack; $mw->Button( -text => 'Quit Main', -command => sub{$mw->destroy}, )->pack; my $top = $mw->Toplevel; $top->Label(-text => 'Top level Window')->pack; $top->Button( -text => 'Exit Top level', -command => sub{$top->destroy}, )->pack; $top->transient($mw); MainLoop;

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh


Comment on Re^3: Error Not a tk object / Problems with multiple files
Download Code
Re^4: Error Not a tk object / Problems with multiple files
by reaper9187 (Scribe) on Oct 16, 2012 at 12:03 UTC
    Oh yes . it is Perl/Tk.
    Thank you so much for the prompt reply..
      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..

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999298]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-07-13 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (250 votes), past polls