为什么 Tkinter.TopLevel.wm_geometry() 返回“1x1+0+0"? [英] Why does Tkinter.TopLevel.wm_geometry() return "1x1+0+0"?
问题描述
我有一个 Tkinter 根窗口和一个 tk.Toplevel.当我在根窗口和顶层调用 .wm_geometry()
时,它们都返回字符串 1x1+0+0
,即使我的窗口在屏幕上可见.
I have a Tkinter root window and a tk.Toplevel. When I call .wm_geometry()
on the root window and on the Toplevel, they both return the string 1x1+0+0
, even when my windows are visible on the screen.
我的代码的精简版本是:
A condensed version of my code is:
root = tk.Tk()
root.withdraw()
window = tk.Toplevel(root)
window.withdraw()
# add things to this window
root.after(0, execute_next_instruction)
root.mainloop()
def execute_next_instruction():
window.wm_geometry() # "1x1+0+0"
推荐答案
原来是因为我使用了tk.Tk.after(0, execute_next_instruction)
,所以调用获取窗口几何在窗口准备好之前,信息仍在执行.修复方法是使用 tk.Tk.after(1, execute_next_instruction)
.
It turns out that because I was using tk.Tk.after(0, execute_next_instruction)
, the calls to get the window geometry information were still executing before the window was ready. The fix was to use tk.Tk.after(1, execute_next_instruction)
.
这篇关于为什么 Tkinter.TopLevel.wm_geometry() 返回“1x1+0+0"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!