Here's two ways you can stop the button from being repeatedly invoked:
Checking for existence:
use strict; use warnings; use Tk; my $mw = MainWindow->new(); my $single_tl; $mw->Button(-text => 'Open Toplevel', -command => sub { if (Exists($single_tl)) { $mw->messageBox(-message => 'Already Open'); } else { $single_tl = $mw->Toplevel(); $single_tl->Button(-text => 'Close', -command => sub { $single_tl->destroy(); }, )->pack(); } }, )->pack(); $mw->Button(-text => 'Exit', -command => sub { exit })->pack(); MainLoop;
Using a grab:
use strict; use warnings; use Tk; my $mw = MainWindow->new(); $mw->Button(-text => 'Open Toplevel', -command => sub { my $single_tl = $mw->Toplevel(); $single_tl->Button(-text => 'Close', -command => sub { $single_tl->destroy(); }, )->pack(); $single_tl->grab(); }, )->pack(); $mw->Button(-text => 'Exit', -command => sub { exit })->pack(); MainLoop;
Please note that both of these pieces of code are self-contained, will run without requiring further code and are not cluttered with irrelevant, cosmetic instructions (e.g. changing colours, resizing windows and so on). Please read How do I post a question effectively? paying particular attention to "... a minimal script that reproduces your problem ..."
Furthermore, your zig-zag indentation is not particularly easy to ready. Please choose an indentation style and use it. perlstyle may help you with this.
You've received help on quite a few questions you've posted in recent weeks. Consider helping those who are helping you.
-- Ken
In reply to Re: opening multiple sub-windows in perl tk
by kcott
in thread opening multiple sub-windows in perl tk
by vr786
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |