in reply to error:not a Tk object error, please help.

This appears to do what you want:

#!/usr/bin/perl use strict; use warnings; use Tk; my $t1; my $mw = MainWindow->new; $mw->title( "MainWindow" ); my $spawn_button = $mw->Button( -text => "Toplevel", -command => \&do_Toplevel )->pack; MainLoop; sub do_Toplevel { $spawn_button->configure(-state => 'normal'); if ( !Exists( $t1 ) ) { $t1 = $mw->Toplevel(); sub { $t1->withdraw; $spawn_button->configure(-state => 'disabled'); $t1->geometry('300x100+100+100'); $t1->title( "Toplevel" ); $t1->Button( -text => "Close", -command => sub { $t1->withdraw; $spawn_button->configure(-state => 'normal'); } )->pack; } } else { $t1->deiconify(); $t1->raise(); } }

update: Strange, but the script on my computer worked, but after downloading the script from perlmonks---it doesn't work. Sorry...

update: Fixed, it should work now.

Replies are listed 'Best First'.
Re^2: error:not a Tk object error, please help.
by Anonymous Monk on May 19, 2008 at 08:26 UTC
    1. Tk::Error: Undefined subroutine &main::do_Toplevel called

    2. You essentially wrote

    if ( !Exists( $t1 ) ) { $t1 = $mw->Toplevel(); sub { # bunch of code that can never get called } }
      I didn't write that. I just had a gut feeling that OnDestroy was causing the problem, so I was focused on that. Otherwise, I agree with you.
        Deleting code to remove the error generated by it can't be a solution.