Python Tkinter:尝试获取小部件大小 [英] Python Tkinter: Attempt to get widget size

查看:2098
本文介绍了Python Tkinter:尝试获取小部件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用 winfo_geometry()函数来查找我窗口的大小,但它最终返回 1x1 + 0 + 0
我也试过了 winfo_height,winfo_width 但我总是收到 1



代码



  from tkinter import * 

root = Tk()

root.geometry('400x600')

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

root.mainloop()




添加一个

print 之前的root.update(),它显示了正确的尺寸。



<$从Tkinter导入的pre> *

root = Tk()

root.geometry('400x600')

root.update()

print(root.winfo_width())
print(root.winfo_height())
print(root.winfo_geo metry())

root.mainloop()


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()

解决方案

You are trying to get the dimensions before the window has been rendered.

Add a root.update() before the prints and it shows the correct dimensions.

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:尝试获取小部件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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