Get Screen Width & Height using Tkinter in Python

INTRODUCTION

Tkinter is a standard graphical user interface (GUI) in python which allows developers to create windows, dialogs , buttons and many more elements. In this python program we use Tkinter library to get the screen width and height .

PROGRAM

Full program to get the screen width and height using Tkinter in Python:
import tkinter as tk

def width():
    w=dimension.winfo_screenwidth()
    return f"Width:{w}" 
def height():
    h=dimension.winfo_screenheight()
    return f"Height:{h}"

dimension=tk.Tk()
dimension.title("SCREEN SIZE")
dimension.geometry("600x400+350+150")
title=tk.Label(text="Screen size: Width And Height",font =("Times New Roman", 15),relief="ridge",borderwidth=5)
obj1=tk.Label(dimension,font="arial 15 bold",relief="groove")
obj2=tk.Label(dimension,font="arial 15 bold",relief="groove")

title.pack()
obj1.pack()
obj2.pack()

obj1.config(text=width())
obj2.config(text=height())

dimension.mainloop()

Program Explanation:

We shall breakdown the program into smaller parts to understand the program better and easily.

Import Tkinter library

import tkinter as tk

This line imports the Tkinter library and names it as ‘tk’, so we can use ‘tk’ in the program wherever required. This makes it easier to write and read the code.

Define function width

def width():
    w = dimension.winfo_screenwidth()
    return f"Width: {w}" 

The width() function gives us the width of the screen using the winfo_screenwidth() method of the Tkinter window (dimension). It returns a formatted string that includes the width value.

Define function height

def height():
    h=dimension.winfo_screenheight()
    return f"Height:{h}"

The height() function gives the screen height using the winfo_screenheight() method and returns a formatted string with the height value.

Initializing The Tkinter Window

dimension = tk.Tk()

This line creates a new tkinter window instance and assign it to the dimension variable.

Defining The Window Title and Geometry

dimension.title("SCREEN SIZE")
dimension.geometry("600x400+350+150")

In the first line , the title() method sets the title in the window to “SCREEN SIZE” which will be displayed in the top left corner of the graphical user interface window.
In the second line, the geometry() method defines the size and the position of the window.

Creating a Label title

title = tk.Label(text="Screen size: Width And Height", font=("Times New Roman", 15), relief="ridge", borderwidth=5)

A label is created with name title. It displays the text “Screen size: Width And Height” in a specified font and style. The relief parameter adds a border effect, and borderwidth
borderwidth specifies the width of the border.

Creating Labels for width & height

obj1 = tk.Label(dimension, font="arial 15 bold", relief="groove")
obj2 = tk.Label(dimension, font="arial 15 bold", relief="groove")

Next, we are creating two labels for width and height. it is used to display the screen width and height.
They are styled with bold Arial font and a groove relief .

Packing the Widgets

title.pack()
obj1.pack()
obj2.pack()

The pack() method is called on the labels to arrange them vertically in the window. This method automatically handles the placement of widgets in the available space

Configuring the Label Texts

obj1.config(text=width())
obj2.config(text=height())

The config() method updates the text of obj1 and obj2 with the values returned by the width() and height() functions, respectively.
This dynamic display updates the labels to show the actual dimensions of the screen.

Start the Tkinter Event Loop

dimension.mainloop()

The mainloop() method is called on the dimension window. This starts the Tkinter event loop, which keeps the window active and responsive, allowing it to display and handle user interactions. The program continues running until the window is closed by the user.

Output

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top