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; } } ##############################################################

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

In reply to Re^2: Callbacks using TK::Minicalendar by zentara
in thread Callbacks using TK::Minicalendar by reaper9187

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.