Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In tkinter, why winfo_height() always return 1?

Tags:

python

tkinter

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
like image 442
YourTeddy Avatar asked Jan 20 '15 08:01

YourTeddy


People also ask

How to get the current width and height of the Tkinter window?

In order to get the width and height of the tkinter window, we can use winfo_width () and winfo_height () helper methods that help to grab the current width and height of the tkinter window. Running the above code will display a window that contains a button.

What is the use of winfo_ismapped () method In Tkinter?

Tkinter provides numerous of universal widget methods or basic widget methods which works almost with all the available widgets. winfo_ismapped () method – This method is used to check whether the specified widget is visible or not.

How to check if widget is visible or not In Tkinter?

Tkinter provides numerous of universal widget methods or basic widget methods which works almost with all the available widgets. This method is used to check whether the specified widget is visible or not. Return Value: Returns True if widget is visible (or mapped), otherwise returns False.

What is winfo fpixels window number?

winfo fpixels window number Returns a floating-point value giving the number of pixels in window corresponding to the distance given by number. Number may be specified in any of the forms acceptable to Tk_GetScreenMM, such as ``2.0c'' or ``1i''. The return value may be fractional; for an integer value, use winfo pixels.


1 Answers

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
like image 141
Reut Sharabani Avatar answered Oct 13 '22 01:10

Reut Sharabani