Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: opening multiple sub-windows in perl tk

by zentara (Archbishop)
on Nov 18, 2010 at 16:23 UTC ( [id://872295]=note: print w/replies, xml ) Need Help??


in reply to opening multiple sub-windows in perl tk

Here are a couple of scripts to show how to make a reusable toplevel window. The first is very simple, the second shows repacking the toplevel with new widgets.
#!/usr/bin/perl use warnings; use strict; use Tk; my $tl; 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=>'disabled'); if ( !Exists( $tl ) ) { $tl = $mw->Toplevel(); $tl->protocol('WM_DELETE_WINDOW' => sub { print "do nothing here\n"; #prevents destruction +of $tl #by WM control }); $tl->geometry('300x100+100+100'); $tl->title( "Toplevel" ); $tl->Button( -text => "Close", -command => sub { $tl->withdraw; $spawn_button->configure(-state=>'normal'); } )->pack; } else { $tl->deiconify(); $tl->raise(); } }
more complex.....and useful
#!/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+100+100'); $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+100+100'); $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(); }

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-24 22:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found