Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

center window on screen

413 views
Skip to first unread message

Dominique Marchand

unread,
Oct 14, 2002, 10:33:06 AM10/14/02
to

Hi everybody,

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

Gilbert Grape

unread,
Oct 14, 2002, 11:25:35 AM10/14/02
to
Hi,

I do it so:

my $FMain = Tk::MainWindow->new();

$FMain->withdraw();
$FMain->transient();
$FMain->Popup();

Tk::MainLoop();


Bye bye
Gilbert

Michael Peuser (h)

unread,
Oct 14, 2002, 1:42:31 PM10/14/02
to

> I need help to center a MainWindow on screen with Tk . I want my main
> window open on screen center in all case.


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


Dominique Marchand

unread,
Oct 15, 2002, 2:43:16 AM10/15/02
to
Gilbert Grape wrote:

thanks a lot! it's good
bye
Dom

Dominique Marchand

unread,
Oct 15, 2002, 2:50:35 AM10/15/02
to
"Michael Peuser (h)" wrote:

thanks for your explanations that is a good complment of "introduction to
perl/tk".
bye
dom

Jan Engelhardt

unread,
Oct 22, 2002, 2:14:06 PM10/22/02
to Dominique Marchand
>Hi everybody,
>
>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,

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

0 new messages