How do I get the width and height of a Tkinter window?

TkinterServer Side ProgrammingProgramming

Tkinter initially creates an instance of a window which is a container that contains all the widgets and the elements. If we want to resize the window, we can use the geometry method by defining the value of width and height.

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.

Example

# Import tkinter library
from tkinter import *
# Create an instance of tkinter frame
win = Tk()
# Set the Geometry
win.geometry("750x250")
def print_width():
   print("The width of Tkinter window:", win.winfo_width())
   print("The height of Tkinter window:", win.winfo_height())
# Create a Label
Label(win, text="Click the below Button to Print the Height and width of the Screen", font=('Helvetica 10 bold')).pack(pady=20)
# Create a Button for print function
Button(win, text="Click", command=print_width).pack(pady=10)
win.mainloop()

Output

Running the above code will display a window that contains a button.

If you click the button, it will print the current width and height of the tkinter window on the console.

The width of Tkinter window: 750
The height of Tkinter window: 250
raja
Published on 15-Apr-2021 17:15:08
Advertisements