Get Screen Width And Height Using Tkinter In Python

INTRODUCTION

In this Python program we will learn how to get the screen width and height using Tkinter. This program uses the Tkinter library to create a GUI(Graphical User Interface) that shows the screen’s width and height. The window contains a title and two buttons which allow users to display the screen dimensions when the button is clicked.
The program initializes the Tkinter window and sets its size to match the screen dimensions. Two functions fetch the screen width and height using winfo_screenwidth() and winfo_screenheight(), updating the label with the respective value.
The buttons and labels are organized vertically using pack(), and the mainloop() method keeps the window responsive until closed. This demonstrates a simple interactive desktop app using Python.

PROGRAM

Here is the full program to get screen width and height:

import tkinter as tk
def show_width():
    screen_width=r.winfo_screenwidth()
    label.config(text=f"Screen Width:{screen_width}")
    
def show_height():
    screen_height=r.winfo_screenheight()
    label.config(text=f"Screen Height:{screen_height}")
    
r=tk.Tk()
r.title("Screen Dimension")
r.geometry(f"{r.winfo_screenwidth()}x{r.winfo_screenheight()}")

title=tk.Label(text="TO GET SCREEN HEIGHT AND WIDTH",font="Arial 15 bold",relief="ridge",borderwidth=10)
title.pack()

label=tk.Label(text="Press A Button To Get Screen Dimension",font=("Times New Roman",15))
label.pack(pady=20)

width_button=tk.Button(r,text="GET WIDTH",command=show_width,font=("Arial 13"),width=20,relief="groove",borderwidth=10)
width_button.pack(pady=10)

height_button=tk.Button(r,text="GET HEIGHT",command=show_height,font=("Arial 13"),width=20,relief="groove",borderwidth=10)
height_button.pack(pady=10)
r.mainloop()

now lets breakdown the program so that we can understand how the program works

Import the Tkinter Library:

import tkinter as tk

This code starts by importing the tkinter library, which is the standard library for creating GUI applications in Python

Define Functions:

Function show_width():
This function retrieves the screen’s width using winfo_screenwidth() and updates the label to display this value.

def show_width():
    screen_width=r.winfo_screenwidth()
    label.config(text=f"Screen Width:{screen_width}")

Function show_height():
Similar to show_height(), this function retrieves the screen’s height using winfo_screenheight() and updates the label.

def show_height():
    screen_height=r.winfo_screenheight()
    label.config(text=f"Screen Height:{screen_height}")

Create the Main Window:

r = tk.Tk()
r.title("Screen Dimension")
r.geometry(f"{r.winfo_screenwidth()}x{r.winfo_screenheight()}")

r = tk.Tk(): This initializes the main window (the root widget).
r.title(“Screen Dimension”): Sets the title of the window.
r.geometry(: This sets the size of the window to match the screen’s dimensions (full screen).

Create the Title Label:

title = tk.Label(text="TO GET SCREEN HEIGHT AND WIDTH", font="Arial 15 bold", relief="ridge", borderwidth=10)
title.pack(pady=10)

1.A label widget is created to display a title message.
2.The font parameter specifies the font style and size.
3.relief and borderwidth: Give the label a styled border.
4.pack(pady=10): Places the label in the window with 10 pixels of vertical padding

Create the Instruction Label:

label = tk.Label(text="Press A Button To Get Screen Dimension", font=("Times New Roman", 15))
label.pack(pady=20)

1.Another label is created to instruct the user to press a button.
2.pack(pady=20): Adds more vertical space above and below the label.

Create Buttons:

width_button = tk.Button(r, text="GET WIDTH", command=show_width, font=("Arial", 13), width=20, relief="groove", borderwidth=10)
width_button.pack(pady=10)

height_button = tk.Button(r, text="GET HEIGHT", command=show_height, font=("Arial", 13), width=20, relief="groove", borderwidth=10)
height_button.pack(pady=10)

1.Two button widgets are created:

  • width_button: When clicked, it calls show_width().
  • height_button: When clicked, it calls show_height().

2.Both buttons have a specified text, font, width, and visual style (using relief and borderwidth).

3.pack(pady=10) adds vertical space around each button.

Start the Main Loop:

r.mainloop()

This line starts the Tkinter event loop, which keeps the window open and responsive to user inputs (like button clicks). The program will continue to run until the window is closed.

OUTPUT

OUTPUT 1:HOME PAGE

OUTPUT 2:GET WIDTH

OUTPUT 3:GET HEIGHT

CONCLUSION

This program uses Tkinter to create a simple GUI where users can click buttons to display their screen’s width and height. It demonstrates event-driven programming, with functions triggered by user actions. The label dynamically updates based on the button clicked. This example highlights basic Tkinter elements for building responsive and interactive interfaces.

Leave a Comment

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

Scroll to Top