为什么“几何()"方法有延迟吗? [英] Why does the "geometry()" method work with a delay?

查看:22
本文介绍了为什么“几何()"方法有延迟吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我运行这个 Python 代码:

from Tkinter import *;w = Tk();w.geometry("640x480");打印( w.geometry() )

我会得到1x1+0+0"的输出.但是,如果我启动解释器并将其作为两个单独的执行命令,我会得到完全不同的输出:

<预><代码>>>>从 Tkinter 进口 *;w = Tk();w.geometry("640x480")''>>>w.geometry()'640x480+101+73'

似乎几何图形不是立即应用的,还需要其他东西:(.也许有人知道我需要做什么才能更新几何图形到位?我需要它来正确居中/定位主窗口和子窗口.

解决方案

在窗口(或小部件)上调用 update_idletasks() 将强制更新其几何图形.

这是来自 Tkinter 参考的一小段文本:

<块引用>

在应用程序更新其空闲任务之前,几何图形是不准确的.特别是,所有几何图形最初都是1x1+0+0",直到小部件和几何图形管理器协商好它们的位置.

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.

解决方案

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.

这篇关于为什么“几何()"方法有延迟吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆