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

UPDATE:

I asked this question in another forum, and got the answer from Jack D. It appears that there is a bug in the Popup method, and it only uses the requested size. This should be fixed in the next version, but you can fix it yourself by adding the following three lines (with #JD#) to the Popup subroutine of C:\Perl\site\lib\Tk\Wm.pm and C:\Perl\site\lib\auto\Tk\Wm\Popup.al

sub Popup { my $w = shift; $w->configure(@_) if @_; $w->idletasks; my ($mw,$mh) = ($w->reqwidth,$w->reqheight); my ($minwidth,$minheight) = $w->minsize; #JD# $mw = $minwidth if (defined $minwidth and $minwidth > $mw); #JD# $mh = $minheight if (defined $minheight and $minheight >$mh); #JD# my ($rx,$ry,$rw,$rh) = (0,0,0,0); ...
Hello World!

With reference to the code below, if I comment out the

$mw -> minsize (qw(600 400));
line, my main window appears centered in the centre of the screen. With that line in, some part of the main window (I don't know what) is positioned relative to the centre of the screen. Can anyone tell me what I need to do to get it to appear in the centre of the screen after issuing a minsize command? I've tried every combination and order I can think of!

Thanks,

Spike.

use strict; use warnings; use Tk; my $mw = MainWindow -> new; $mw -> minsize (qw(600 400)); $mw -> withdraw; for (1..10) { my $button = $mw -> Label (-text => "Label $_") -> pack; } $mw -> update; $mw -> idletasks; $mw -> Popup( -overanchor => 'c', -popanchor => 'c', ); MainLoop;

Replies are listed 'Best First'.
Re: Tk centering main window on screen.
by gri6507 (Deacon) on May 27, 2004 at 15:36 UTC
    Maybe this will help.

    Update I got this from searching here for "Tk center screen".

      Thanks for that. I suppose I should have said in my original message that I know I can position it using the very long winded geometry method of working out the screen size, then the window size, etc.

      It's just so annoying that I can use Popup with no parameters to get the main window in the centre of the screen, but once I've issued a minsize command, it all goes wrong.

      Spike.

Re: Tk centering main window on screen.
by zentara (Cardinal) on May 27, 2004 at 17:08 UTC
    I just tried your code, and didn't see any differences in the location of the upper left corner of the popup window, with or without minsize. So I guess you are making an assumption, based on the appearance of the smaller popup seeming to be centered. The anchor is the upper left corner of the window, not the center of the window.

    I'm using linux Tk804.027


    I'm not really a human, but I play one on earth. flash japh
      OK, consider the following example. The windows are exactly the same size, yet appear in different positions. What do I need to to do the 2nd popup to get it to be centered in the main window?

      use strict; use warnings; use Tk; my $mw = MainWindow -> new; $mw -> minsize (qw(600 400)); my $button = $mw -> Button ( -text => '1st popop', -command => \&popup1, ) -> pack; my $other_button = $mw -> Button ( -text => '2nd popup', -command => \&popup2, ) -> pack; MainLoop; sub popup1 { print "Main window geometry = ".$mw -> geometry."\n"; my $popup1 = $mw -> Toplevel; $popup1 -> withdraw; for (1..10) { my $label = $popup1 -> Label ( -text => "This is a very very very long label", ) -> pack; } $popup1 -> Popup ( -popover => $mw, -overanchor => 'c', -popanchor => 'c', ); print "1st popup geom = ".$popup1 -> geometry."\n\n"; } sub popup2 { print "Main window geometry = ".$mw -> geometry."\n"; my $popup2 = $mw -> Toplevel; $popup2 -> withdraw; my $label = $popup2 -> Label ( -text => "This is a label", ) -> pack; $popup2 -> minsize (qw(162 190)); $popup2 -> Popup ( -popover => $mw, -overanchor => 'c', -popanchor => 'c', ); print "2nd popup geom = ".$popup2 -> geometry."\n\n"; }
Re: Tk centering main window on screen.
by eserte (Deacon) on Jun 01, 2004 at 17:46 UTC
    The problem seems to be that the minimum size is forced by the window manager, not by the Tk internals. So Tk only sees the requested size of the label and calculates the position according to this size. After (or while) the window gets mapped, the window manager forces the window to be larger according to minsize.

    A fix could be to a) set explicitely the window size to the minimum and b) to not allow pack propagation before the window is mapped. Something like this, to be inserted before the Popup call:

    $popup2->GeometryRequest(162,190); $popup2->packPropagate(0);
    After the Popup, you could turn pack propagation on again.
Re: Tk centering main window on screen.
by zentara (Cardinal) on May 28, 2004 at 15:03 UTC
    What do I need to to do the 2nd popup to get it to be centered in the main window?

    Well you could always post this in the newsgroup comp.lang.perl.tk where the Tk internals experts hangout.

    Personally, I think you are expecting too much from the popup widget. I think the big problem is that popup expects to be able to handle all the screen details in it's own way, and as soon as you try to add "defaults" like minsize, popup figures you are taking control, and expects you to calculate the position.

    As to how you would do it..... just calculate it. Like:

    sub CenterWindow { my($window, $width, $height) = @_; $window->idletasks; $width = $window->reqwidth unless $width; $height = $window->reqheight unless $height; my $x = int(($window->screenwidth / 2) - ($width / 2)); my $y = int(($window->screenheight / 2) - ($height / 2)); $window->geometry($width . "x" . $height . "+" . $x . "+" .$y); }

    As far as I'm concerned that is EASIER than dealing with popup. I think popup was designed to display quick messages in view, it wasn't intended to be a "general purpose centering method".

    If you want this badly enough, you could hack the Tk c code, to get it to do what you want, submit a patch, and it may be included in the next version. But I would ask about it on comp.lang.perl.tk first. As with most changes, it may break something else.


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

    Edited by Chady -- closed <b> tag