warpod has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to position a newly created Toplevel widget relative to specific widget in another Toplevel widget?

for example:
#!/usr/bin/perl -w use strict; use Tk; my $w = MainWindow->new; my $button; $button = $w->Button(-text => "click me", -command => sub { my $mw = $w->Toplevel; # here I need somehow position the $mw over the $button $mw->Label(-text => "hello")->pack(); })->pack; MainLoop;
when I click the button I need a new window x/y absolute coordinates to be positioned right at the x/y absolute coordinates of the button, so the new window will overlap the button every time I click it.

UPD: problem solved using 'geometry' method
code for commented line is something like this:
my @wg = ($w->geometry =~ /\+(\d+)\+(\d+)/); my @bg = ($button->geometry =~ /\+(\d+)\+(\d+)/); $wg[$_] += $bg[$_] for (0..@wg-1); $mw->geometry("+$wg[0]+$wg[1]");

Replies are listed 'Best First'.
Re: Perl/Tk widget coordinates, simple question
by AnomalousMonk (Archbishop) on Jan 05, 2010 at 17:48 UTC

    I haven't used it myself, but you might investigate the Tk  place geometry manager, in particular its  -in -x -y -relx -rely -anchor options. The caveat is that you may need to use  place as the geo. manager for the entire application to avoid the possibility of duelling managers.

      I think I found out the way using "geometry" method (I'm new to Tk, so did not find this method in first place)

      replacing comment line with this:
      my @wg = ($w->geometry =~ /\+(\d+)\+(\d+)/); my @bg = ($button->geometry =~ /\+(\d+)\+(\d+)/); $wg[$_] += $bg[$_] for (0..@wg-1); $mw->geometry("+$wg[0]+$wg[1]");
      has solved my problem
Re: Perl/Tk widget coordinates, simple question
by zentara (Cardinal) on Jan 06, 2010 at 12:00 UTC
    For the best control in placment, use a Tk::Canvas. Usually you don't embed other widgets into a canvas, you make your own, with rectangles, and circles, etc..... but this works precisely
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my $width = 500; my $height = 500; $mw->fontCreate('big', -family=>'courier', -weight=>'bold', -size=>int(-18*18/14)); my $canv = $mw->Canvas( -bg => 'lightsteelblue', -relief => 'sunken', -width => $width, -height => $height)->pack(-expand => 1, -fill => 'both'); my $text = $mw->Scrolled('Text', -bg=>'lightyellow', -scrollbars=>'osoe', ); my $textcontainer = $canv->createWindow( $width/2, $height/2, -window => $text, -width => $width -200, -height => $height-200, -state => 'normal'); my $button = $mw->Button( -bg=>'lightblue', -text=>'Push Me', -command=> sub{ $text->insert('end',time."\n")}, ); my $butcontainer = $canv->createWindow( 125, 25, -window => $button, -width => 125, -height => 25, -state => 'normal'); my $ctext = $canv->createText( 300,20, -font=>'big', -fill=> 'white', -text=>'la di da'); bunchOfBoxes(); $text->focus; MainLoop(); sub bunchOfBoxes { for(0..10){ my $window = $canv->Checkbutton(-text=> $_); $canv->createWindow(450, 20 + ($_ * 20), -window=> $window); } }

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