Slideshow Viewer in Python

Slideshow Viewer in Python

Slideshow Viewer in Python – A Complete and Practical Guide

Introduction

A Slideshow Viewer is a desktop application that automatically or manually displays a sequence of images one after another. We see slideshow viewers everywhere: photo galleries, presentation tools, digital signboards, learning kiosks, and even screensavers.

Building a slideshow viewer in Python is an excellent real-world project for beginners as well as intermediate learners because it combines GUI development, file handling, event handling, and basic application design logic.

In this article, we will build a Python Slideshow Viewer from scratch and deeply understand how it works. This guide is written in a human-friendly, student-oriented, and easy-to-follow teaching style.


What Is a Slideshow Viewer?

A slideshow viewer is an application that:

  • Loads multiple images from a folder
  • Displays one image at a time
  • Allows navigation using Next and Previous buttons
  • Automatically changes images after a fixed interval
  • Supports resizing or fullscreen mode

In simple terms, it is a smart image viewer that knows which image comes next and when to show it.


Why Build a Slideshow Viewer in Python?

  • Beginner-friendly project
  • Real-world desktop application
  • Learn Tkinter GUI fundamentals
  • Understand logic building and event handling
  • Easy to extend with new features

Tools and Libraries Used

1. Python

The core programming language used for logic and structure.

2. Tkinter

Tkinter is Python’s built-in GUI library. It provides widgets such as windows, labels, buttons, and frames.

3. Pillow (PIL)

Pillow is used for image handling like loading, resizing, and converting images for Tkinter.

pip install pillow

Project Features

  • Main application window
  • Image display area
  • Next and Previous buttons
  • Automatic slideshow mode
  • Clean and simple UI

Project Folder Structure


slideshow_viewer/
│
├── images/
│   ├── img1.jpg
│   ├── img2.png
│   └── img3.jpg
│
└── slideshow.py

Understanding the Core Logic

Image List

images = ["img1.jpg", "img2.jpg", "img3.jpg"]

Image Index

current_index = 0

Navigation Logic

Next → index increases
Previous → index decreases
Modulo (%) ensures looping without errors


Step-by-Step Implementation

Step 1: Import Required Libraries


import tkinter as tk
from PIL import Image, ImageTk
import os

Step 2: Create the Main Window


root = tk.Tk()
root.title("Python Slideshow Viewer")
root.geometry("800x500")
root.resizable(False, False)

Step 3: Load Images from Folder


image_folder = "images"

image_files = [
    file for file in os.listdir(image_folder)
    if file.lower().endswith((".png", ".jpg", ".jpeg", ".bmp", ".gif"))
]

image_files.sort()
images = []

for file in image_files:
    img = Image.open(os.path.join(image_folder, file))
    img = img.resize((700, 400), Image.LANCZOS)
    images.append(ImageTk.PhotoImage(img))

Step 4: Display Image


current_index = 0
image_label = tk.Label(root, image=images[current_index])
image_label.pack(pady=10)

Step 5: Navigation Functions


def next_image():
    global current_index
    current_index = (current_index + 1) % len(images)
    image_label.config(image=images[current_index])

def prev_image():
    global current_index
    current_index = (current_index - 1) % len(images)
    image_label.config(image=images[current_index])

Step 6: Buttons


button_frame = tk.Frame(root)
button_frame.pack()

tk.Button(button_frame, text="Previous", command=prev_image).pack(side="left", padx=10)
tk.Button(button_frame, text="Next", command=next_image).pack(side="right", padx=10)

Step 7: Automatic Slideshow


def auto_slideshow():
    next_image()
    root.after(2000, auto_slideshow)

root.after(2000, auto_slideshow)

Step 8: Run Application

root.mainloop()

Common Problems and Solutions

  • Images not showing: Check folder path and file formats
  • App freezing: Never use time.sleep() in Tkinter
  • Large images: Always resize images before display

Possible Enhancements


Conclusion

Building a Slideshow Viewer in Python is more than just displaying images. It helps you understand how real desktop applications work using GUI, logic, and events.

This project is perfect for students, teachers, and beginners who want hands-on experience with Python GUI development.


Final Complete Code>

import tkinter as tk
from PIL import Image, ImageTk
import os

# ---------------- Window ----------------
root = tk.Tk()
root.title("Python Slideshow Viewer")
root.geometry("800x500")
root.resizable(False, False)

# ---------------- Load Images ----------------
image_folder = "images"
image_files = os.listdir(image_folder)

images = []

for file in image_files:
    img = Image.open(os.path.join(image_folder, file))
    img = img.resize((700, 400))
    images.append(ImageTk.PhotoImage(img))

if not images:
    raise Exception("No images found in images folder")

# ---------------- Image Index ----------------
current_index = 0

# ---------------- Image Label ----------------
image_label = tk.Label(root, image=images[current_index])
image_label.pack(pady=10)

# ---------------- Functions ----------------
def next_image():
    global current_index
    current_index = (current_index + 1) % len(images)
    image_label.config(image=images[current_index])


def prev_image():
    global current_index
    current_index = (current_index - 1) % len(images)
    image_label.config(image=images[current_index])


def auto_slideshow():
    next_image()
    root.after(2000, auto_slideshow)  # 🔁 repeat every 2 seconds

# ---------------- Buttons ----------------
button_frame = tk.Frame(root)
button_frame.pack()

prev_btn = tk.Button(button_frame, text="Previous", command=prev_image)
prev_btn.pack(side="left", padx=10)

next_btn = tk.Button(button_frame, text="Next", command=next_image)
next_btn.pack(side="right", padx=10)

# ---------------- Start Slideshow ----------------
root.after(2000, auto_slideshow)

root.mainloop()

OUTPUT:

Frequently Asked Questions (FAQ)

1. Is this slideshow viewer suitable for absolute beginners?

Yes. This project is designed for beginners who understand basic Python concepts like variables, functions, and loops. Tkinter is easy to learn and ideal for a first GUI project.

2. Do I need an internet connection to run this application?

No. The slideshow viewer works completely offline. Once Python and Pillow are installed, no internet connection is required.

3. Which image formats are supported?

Using the Pillow library, the application supports JPG, JPEG, PNG, BMP, and GIF image formats. PNG and JPG are recommended for best performance.

4. Why do we use the Pillow library instead of Tkinter alone?

Tkinter has limited image support. Pillow allows resizing, supports more image formats, and converts images into a form that Tkinter can display properly.

5. What is the purpose of the after() method in Tkinter?

The after() method schedules tasks without freezing the GUI. It is used for the automatic slideshow feature to change images at fixed intervals.

6. Can I pause or stop the automatic slideshow?

Yes. You can add a pause/play button by controlling the after() loop using a boolean flag. This is a common and recommended enhancement.

7. Why is the modulo (%) operator used in image navigation?

The modulo operator ensures the slideshow loops continuously. When the last image is reached, it automatically returns to the first image without causing errors.

8. Can this slideshow viewer run in fullscreen mode?

Yes. Tkinter supports fullscreen using root.attributes('-fullscreen', True). You can also bind the Escape key to exit fullscreen mode.

9. Is this project suitable for school or college submission?

Absolutely. This project demonstrates GUI programming, logical thinking, and real-world application design, making it ideal for academic submissions.

10. How can I convert this into a professional desktop application?

You can add animations, keyboard controls, captions, background music, and package it as an executable file using tools like PyInstaller.


Article By: Sandeep Gupta
Category: Python Desktop Application Development

Previous Post Next Post

Contact Form