在 tkinter 中,为什么 winfo_height() 总是返回 1? [英] In tkinter, why winfo_height() always return 1?

查看:25
本文介绍了在 tkinter 中,为什么 winfo_height() 总是返回 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是最简单的例子.

#py3从 tkinter 导入 *tk = tk()画布 = 画布(tk,宽度= 500,高度= 400)canvas.wininfo_height()#In [4]: canvas.winfo_height()#输出[4]:1

解决方案

你必须打包 在获取其高度之前窗口中的画布元素.返回的高度是实际高度.

<预><代码>>>>从 tkinter 导入 *>>>tk = tk()>>>画布 = 画布(tk,宽度= 500,高度= 400)>>>canvas.wininfo_height()1>>>画布.pack()>>>canvas.wininfo_height()402

This is the most easy example.

#py3
from tkinter import *
tk = Tk()
canvas = Canvas(tk, width= 500 , height = 400)
canvas.winfo_height()
#In [4]: canvas.winfo_height()
#Out[4]: 1

解决方案

You have to pack the canvas element in the window before getting it's height. The height return is the actual height.

>>> from tkinter import * 
>>> tk = Tk()
>>> canvas = Canvas(tk, width= 500 , height = 400)
>>> canvas.winfo_height()
1
>>> canvas.pack()
>>> canvas.winfo_height()
402

这篇关于在 tkinter 中,为什么 winfo_height() 总是返回 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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