geometry() method works with delay. Maybe anyone knows why?

29 views
Skip to first unread message

Grigory Petrov

unread,
Jan 19, 2012, 5:27:19 PM1/19/12
to Tk Documentation and Resources
Hello.

If i run this Python code: "from Tkinter import *; w = Tk();
w.geometry( "640x480" ); print( w.geometry() )" i will get "1x1+0+0"
output. But if i start interpreter and execute this as two separate
commands, i will get completely different output:

>>> from Tkinter import *; w = Tk(); w.geometry( "640x480" )
''
>>> w.geometry()
'640x480+101+73'

It seems geometry is not applied instantly, something else is needed :
(. Maybe anyone knows what i need to do in order to update geometry
inplace? I need it to correctly center/position main and child windows.

Mark Roseman

unread,
Jan 19, 2012, 6:53:17 PM1/19/12
to tkd...@googlegroups.com, Grigory Petrov
The geometry method will try to get the actual size of the window. The actual geometry calculations are not done immediately but in the event loop at an idle time (it would be pretty wasteful otherwise when you have a pile of widgets being put together). Therefore you need to force Tk to do the calculation. To do this, insert:

Tk.update_idletasks(w);

before you print the geometry.

Hope this helps
Mark

> --
> You received this message because you are subscribed to the Google Groups "Tk Documentation and Resources" group.
> To post to this group, send email to tkd...@googlegroups.com.
> To unsubscribe from this group, send email to tkdocs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/tkdocs?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages