How to find out the current widget size in tkinter?

PythonTkinter

Python Problem Overview


I'm using Python and Tkinter, and I need to know the current dimensions (width, height) of a widget.

I've tried somewidget["width"], but it returns only a fixed value, and is not updated whenever the widget size changes (e.g. when the window is resized).

Python Solutions


Solution 1 - Python

somewidget.winfo_width() and somewidget.winfo_height() give 1. You need to update Tk (issue tk.update()) before getting these values.

Solution 2 - Python

Use somewidget.winfo_width() and somewidget.winfo_height() to get the actual widget size, the somewidget['width'] property is only a hint given to the geometry manager.

Solution 3 - Python

You can use the function somewidget.winfo_reqheight() for height and somewidget.winfo_reqwidth() for width, but first don't forget to call the update function of the widget you want to know the dimension somewidget.update(). If you do not call the update function you will get the default value 1.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDenilson Sá MaiaView Question on Stackoverflow
Solution 1 - PythonApostolosView Answer on Stackoverflow
Solution 2 - PythonLie RyanView Answer on Stackoverflow
Solution 3 - PythonJose Luis Bracamonte AmavizcaView Answer on Stackoverflow