In tkinter, why winfo_height() always return 1?

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

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 

回答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 


回答2:

If it doesn't work by using pack() function, you can try to add canvas.update() after using canvas.pack().



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!