Python的Tkinter的:试图获取部件尺寸(Python Tkinter: Attempt t

2019-07-03 18:04发布

I am trying to find the size of my window by using the winfo_geometry() function but it ends up returning 1x1+0+0 I have also tried winfo_height, winfo_width but i keep getting 1

CODE

from tkinter import *

root=Tk()

root.geometry('400x600')

print (root.winfo_width())
print (root.winfo_height())
print (root.winfo_geometry())

root.mainloop()

Answer 1:

您正在试图获得尺寸的窗口已经被渲染之前。

添加root.update()的前print S和它显示正确的尺寸。

from Tkinter import *

root=Tk()

root.geometry('400x600')

root.update()

print (root.winfo_width())
print (root.winfo_height())
print (root.winfo_geometry())

root.mainloop()


文章来源: Python Tkinter: Attempt to get widget size