为什么 tkinter 中的 widget.winfo_rootx() 和 widget.winfo_rootx() 始终为 0? [英] Why are widget.winfo_rootx() and widget.winfo_rootx() always 0 in tkinter?
本文介绍了为什么 tkinter 中的 widget.winfo_rootx() 和 widget.winfo_rootx() 始终为 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
from tkinter import *
root = Tk()
a = Button(text=str(1))
a.place(x=100, y=100)
widget_x, widget_y = a.winfo_rootx(), a.winfo_rooty()
print(widget_x, widget_y)
root.mainloop()
输出总是0 0
.这是为什么,我该如何解决?
The output is always 0 0
. Why is this and how can I fix it?
推荐答案
该值为零,因为您在窗口可见之前获取坐标.您可以通过以下两种方法之一解决此问题:在主循环启动后(使用 after_idle
)不获取坐标,或者通过调用 update强制将窗口绘制到屏幕上代码>.
The value is zero because you're getting the coordinates before the window has been made visible. You can solve this one of two ways: don't get the coordinates until after mainloop has started (with after_idle
), or force the window to be drawn to the screen by calling update
.
示例:
from tkinter import *
root = Tk()
a = Button(text=str(1))
a.place(x=100, y=100)
root.update()
widget_x, widget_y = a.winfo_rootx(), a.winfo_rooty()
print(widget_x, widget_y)
root.mainloop()
这篇关于为什么 tkinter 中的 widget.winfo_rootx() 和 widget.winfo_rootx() 始终为 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文