Hi, for question 1, there is the way to get coordinates of any widget or the mouse cursor for that matter. This is from Mastering Perl/Tk Chapter 13.10
# You can get the coordinates of the upper-left corner of a widget by
# using the x and y methods.
# The coordinates they return are relative to the parent of the widget
+:
$x = $widget->x;
$y = $widget->y;
# for a mouse pointer
my ($x, $y) = $mw->pointerxy;
Then all you need to do is specify a -geometry statement for your toplevel window,
to place it at that position.
For question #2, you need to understand that a Mainwindow or your $top, is initmately connected to the MainLoop statement. When you destroy one, you destroy the eventloop. So you would need to keep your multiple MainLoops, each in their own subroutine or closure, as there can only be 1 MainLoop running at any time( with certain odd exceptions). So it makes me wonder if your basic program design is correct, maybe you could explain better what you are trying to do, and why you need multiple eventloops. If you have the need for making multiple toplevel windows within your program, you should consider letting them all share the same MainLoop, and instead of $top->destroy, you could use $top->packForget
to just remove the calendar from view, allowing the same MainLoop to keep running for your next use.
A MainLoop can have multiple toplevel windows called and or destroyed, as long as the $mw associated with the MainLoop is still intact.
It would look something like this:
You make 1 mainwindow which controls the MainLoop, then you can add as many toplevel windows as often as you like.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $mw = MainWindow->new;
$mw->title( "MainWindow" );
$mw->geometry('400x400+30+30');
$mw->Button( -text => "Toplevel", -command => \&do_Toplevel )->pack();
$mw->waitVisibility;
my $tl;
&do_Toplevel; #initialize it in withdrawn state
MainLoop;
sub do_Toplevel {
my $topx = 300;
my $topy = 100;
# print $mw->geometry,"\n";
my ($xs,$ys,$signx,$xp,$signy,$yp) =
$mw->geometry =~ /(\d+)x(\d+)([+-]+)(\d+)([+-]+)(\d+)/;
# print "$xs,$ys,$signx,$xp,$signy,$yp\n";
my $xpos = int($xp + $xs/2 - $topx/2);
my $ypos = int($yp + $ys/2 - $topy/2);
if ( !Exists( $tl ) ) {
$tl = $mw->Toplevel();
$tl->geometry($topx.'x'.$topy.'+'.$xpos.'+'.$ypos);
$tl->title( "Toplevel" );
#put all your widgets here
$tl->Button(
-text => "Close",
-command => sub { $tl->grabRelease;
$tl->withdraw }
)->pack;
$tl->withdraw;
}
else {
$tl->geometry($topx.'x'.$topy.'+'.$xpos.'+'.$ypos);
$tl->deiconify();
$tl->raise();
$tl->grabGlobal;
}
}
##############################################################
|