How to get the screen size in Tkinter?

TkinterPythonServer Side ProgrammingProgramming

Tkinter initially creates a window or frame object where all the widgets, frame are displayed.

Tkinter components adjust the window size and width according to user-defined geometry.

In order to get the screen size, we can use winfo_screenwidth() which returns the screen width and winfo_screenheight() for the height of the screen in pixels.

Example

In this example, we will print the screen size as the output,

#Import the required libraries
from tkinter import *

#Create an instance of tkinter frame
win= Tk()

#Set the geometry of frame
win.geometry("650x250")

#Get the current screen width and height
screen_width = win.winfo_screenwidth()
screen_height = win.winfo_screenheight()

#Print the screen size
print("Screen width:", screen_width)
print("Screen height:", screen_height)

win.mainloop()

Output

Running the code will display a window and print the screen size.

Screen width: 1366
Screen height: 768
raja
Published on 26-Mar-2021 10:48:07
Advertisements