Get the screen size (width and height) in Tkinter

Introduction

This Python program demonstrates how to obtain the screen width and height using the Tkinter library to create a GUI. The application features a window with a title and two buttons that display the screen dimensions when clicked.

The program initializes a Tkinter window and uses `winfo_screenwidth()` and `winfo_screenheight()` to fetch the screen dimensions, updating labels accordingly. Widgets are organized vertically with the `pack()` method, and `mainloop()` keeps the window responsive until closed. This example illustrates a simple interactive desktop app in Python.

Table of  Contents

  • Import the Tkinter Module
  • Define the Functions
  • Set Up the Main Window
  • Add the Title Label
  • Include the Instruction Label
  • Create the Buttons
  • Initiate the Main Loop

Program

import tkinter as tk

def display_screen_width():
    screen_width = root.winfo_screenwidth()
    info_label.config(text=f"Screen Width: {screen_width}")

def display_screen_height():
    screen_height = root.winfo_screenheight()
    info_label.config(text=f"Screen Height: {screen_height}")

# Set up the main window
root = tk.Tk()
root.title("Screen Dimension")
root.geometry(f"{root.winfo_screenwidth()}x{root.winfo_screenheight()}")

# Create and pack the title label
title_label = tk.Label(root, text="Get Screen Height and Width", font="Arial 15 bold", relief="ridge", borderwidth=10)
title_label.pack(pady=10)

# Create and pack the information label
info_label = tk.Label(root, text="Press a Button to Get Screen Dimension", font=("Times New Roman", 15))
info_label.pack(pady=20)

# Create and pack the buttons
width_button = tk.Button(root, text="Get Width", command=display_screen_width, font=("Arial", 13), width=20, relief="groove", borderwidth=10)
width_button.pack(pady=10)

height_button = tk.Button(root, text="Get Height", command=display_screen_height, font=("Arial", 13), width=20, relief="groove", borderwidth=10)
height_button.pack(pady=10)

# Start the main loop
root.mainloop()

This program creates a simple graphical user interface (GUI) using the Tkinter library. It allows users to get the screen width and height by clicking buttons.

Code Breakdown

Importing the Tkinter Library

This section involves importing the Tkinter library, which is the standard library for creating GUI applications in Py

import tkinter as tk

Defining Functions

  • Function to Display Screen Width: This function retrieves the screen width and updates a label to display this value.
display_screen_width():
screen_width = root.winfo_screenwidth()
info_label.config(text=f"Screen Width: {screen_width}")
  • Function to Display Screen Height: Similar to the width function, this function retrieves the screen height and updates the label.
def display_screen_height():
screen_height = root.winfo_screenheight()
info_label.config(text=f"Screen Height: {screen_height}")

Setting Up the Main Window

This part initializes the main application window, sets its title, and adjusts its size to match the screen dimensions, making it effectively full screen.

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

Creating and Packing the Title Label

A label is created to serve as the title for the window. It includes styling attributes for font, relief, and border width, and is placed in the window with vertical padding for spacing.

title_label = tk.Label(root, text="Get Screen Height and Width", font="Arial 15 bold", relief="ridge", borderwidth=10)
title_label.pack(pady=10)

 

Creating and Packing the Information Label

Another label is created to instruct the user on what actions to take. It also has vertical padding added for better layout.

info_label = tk.Label(root, text="Press a Button to Get Screen Dimension", font=("Times New Roman", 15))
info_label.pack(pady=20)

 

Creating and Packing the Buttons

Two buttons are created:

  • One button retrieves the screen width when clicked.
  • The other button retrieves the screen height when clicked. Both buttons are styled similarly and include padding for spacing.
  • width_button = tk.Button(root, text="Get Width", command=display_screen_width, font=("Arial", 13), width=20, relief="groove", borderwidth=10)
    width_button.pack(pady=10)
    
    height_button = tk.Button(root, text="Get Height", command=display_screen_height, font=("Arial", 13), width=20, relief="groove", borderwidth=10)
    height_button.pack(pady=10)
    
    
    This section starts the event loop, keeping the application running and responsive to user interactions until the window is closed.

Starting the Main Loop

This section starts the event loop, keeping the application running and responsive to user interactions until the window is closed.

Starting the Main Loop

 

Output

 

+---------------------------------------+
| Get Screen Height and Width |
+---------------------------------------+
| Press a Button to Get Screen Dimension |
| |
| [ Get Width ] |
| |
| [ Get Height ] |
+---------------------------------------+

When buttons are clicked, the output on the label will change to:

Screen Width: 1920
Screen Height: 1080

 

Summary

This program effectively demonstrates how to create a basic GUI application using Tkinter. It allows users to obtain and display their screen’s dimensions by clicking buttons, providing a simple yet interactive experience.

Leave a Comment

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

Scroll to Top