即使在使用 pack() 之后 winfo_width() 也返回 1 [英] winfo_width() returns 1 even after using pack()
本文介绍了即使在使用 pack() 之后 winfo_width() 也返回 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在网上搜索过,我也尝试过自己解决,但一直无法解决.即使在渲染小部件之后,winfo 函数也会返回错误的高度和宽度
I have searched online and I have tried solving it on my own but I have not been able to solve it. Even after rendering the widget the winfo functions are returning wrong height and width
from tkinter import *
root = Tk()
frame = Frame(root)
label1 = Label(frame, text = "hello")
label1.pack()
label2 = Label(frame, text = "hello")
label2.pack()
label3 = Label(frame, text = "hello")
label3.pack()
frame.pack()
print(frame.winfo_width(),frame.winfo_height())
#prints "1 1"
root.mainloop()
推荐答案
您需要update_idletasks:
调用所有挂起的空闲任务,不处理任何其他事件.这可用于在必要时执行几何管理和重绘小部件,而无需调用任何回调.
Calls all pending idle tasks, without processing any other events. This can be used to carry out geometry management and redraw widgets if necessary, without calling any callbacks.
frame.pack()
root.update_idletasks()
print(frame.winfo_width(), frame.winfo_height())
这篇关于即使在使用 pack() 之后 winfo_width() 也返回 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文