I need help to center a MainWindow on screen with Tk . I want my main
window open on screen center in all case.
I find only how to know what is the resolution of screen and where is
the main window but i don't how to set x,y to put windows on center of
screen.
Thanks,
PS: Excuse me for my "french" english
I do it so:
my $FMain = Tk::MainWindow->new();
$FMain->withdraw();
$FMain->transient();
$FMain->Popup();
Tk::MainLoop();
Bye bye
Gilbert
The general way to do it is
$widget -> geometry("wxh+x+y")
width (w), height (h), yposition (x), and yposition (y) can be set as string
using above syntax. Position and size can be set independently, e.g.
"+x+y" or "100x200".
Without a parameter the actual size and position is returned in the above
syntax.
Attention: Values are "0" before the widget has manifested itself on the
screen; use
update() or waitVisibility() if unsure.
Note: All values are in pixels.
The screensize can be requested using
$top -> screenwidth()
and
$top -> screenheight()
Thus (e.g.):
$h = $top->screenheight() - 40;
$w = $top->screenwidth() - 40;
$top -> geometry("${w}x$h+20+20")
Mike
thanks a lot! it's good
bye
Dom
thanks for your explanations that is a good complment of "introduction to
perl/tk".
bye
dom
sub center_window {
my $w = shift @_;
if($w->parent() eq "") { #mainwindow
$w->geometry(sprintf "+%d+%d", ($w->screenwidth() - $w->width()) / 2,
($w->screenheight() - $w->height()) / 2);
} else { #toplevel
$w->geometry(sprintf "+%d+%d", ($p->width() - $w->width()) / 2 +
$p->x(), ($p->height() - $w->height()) / 2 + $p->y());
}
$w->update();
}
- Jan Engelhardt