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

center a toplevel

3 views
Skip to first unread message

iu2

unread,
Nov 11, 2009, 3:26:17 PM11/11/09
to
Hi all,

I want to center a toplevel on the screen. I create it, and then
center it, for example:

package require Tk

# center a window on the screen
proc center_win {win} {
wm geometry $win +[expr {([winfo screenwidth $win] - [winfo width
$win]) / 2}]+[\
expr {([winfo screenheight $win] - [winfo height $win]) / 2}]
}

# create some widgets
proc create_toplevel {} {
set f [ttk::labelframe .f -text " Example "]
pack $f -padx 5 -pady 5 -ipadx 5 -ipady 5
set e [ttk::entry $f.e -width 50]
pack $e
for {set i 0} {$i < 5} {incr i} {
set b [ttk::button $f.b$i -text "Button $i"]
pack $b
}
update
}

create_toplevel
center_win .

What happens is that the window is created at an arbitrary position,
and then quickly snapped to the center. My question is, is it possible
to make it appear in the center of the screen in the first place,
without the quick snap.

Thanks

Jeff Godfrey

unread,
Nov 11, 2009, 4:04:10 PM11/11/09
to

The right answer could be OS / Win manager specific, but at least for
me, using Windows XP, I can make the above work correctly by changing
"update" to "update idletasks"...

Jeff

Donal K. Fellows

unread,
Nov 12, 2009, 4:40:25 AM11/12/09
to
On 11 Nov, 20:26, iu2 <isra...@elbit.co.il> wrote:
> What happens is that the window is created at an arbitrary position,
> and then quickly snapped to the center. My question is, is it possible
> to make it appear in the center of the screen in the first place,
> without the quick snap.

Yes. Put it first deliberately a long way off the screen, then call
[update idletasks] (you don't need a full [update]) before calling the
centering code you posted.

Donal.

Gerhard Reithofer

unread,
Nov 12, 2009, 10:47:56 AM11/12/09
to

Isn't the way to [withdraw] it 1st and [deiconify] at end a portable
solution?

--
Gerhard Reithofer
Tech-EDV Support Forum - http://support.tech-edv.co.at

Donal K. Fellows

unread,
Nov 12, 2009, 11:38:42 AM11/12/09
to
On 12 Nov, 15:47, Gerhard Reithofer <gerhard.reitho...@tech-edv.co.at>
wrote:

> Isn't the way to [withdraw] it 1st and [deiconify] at end a portable
> solution?

Depends on the window manager... :-(

Donal.

Gerhard Reithofer

unread,
Nov 13, 2009, 2:10:19 PM11/13/09
to
On Thu, 12 Nov 2009, Donal K. Fellows wrote:

THX

0 new messages