Hi, you will need to make a reusable toplevel window, and repack it with different messages. A few tricks are shown here. The -1-1 geometry placement means "from lower right corner". The toplevel window is reused to prevent memory accumulation from constantly creating new toplevels for each message.
#!/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();
}
|