Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the "geometry()" method work with a delay?

Tags:

python

tkinter

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.

like image 854
grigoryvp Avatar asked Jan 19 '12 22:01

grigoryvp


1 Answers

Calling update_idletasks() on a window (or a widget) will force its geometry to update.

Here's a little text snippet from the Tkinter reference:

The geometry is not accurate until the application has updated its idle tasks. In particular, all geometries are initially "1x1+0+0" until the widgets and the geometry manager have negotiated their positions.

like image 106
D K Avatar answered Nov 01 '22 04:11

D K