It is eye-appealing to watch images slideshows, right? Let’s make our own slideshow app with Python.
Idea:
Python’s Tkinter works best when it comes to GUI. Here, we are showcasing the images over a Tkinter window and then going to set a timer so that the images will slide until we hit the close button.
Super simple, right?
Implementation:
Step 1: Import the modules:
We will need Tkinter and itertools for iterating over the images.
Tkinter: Tkinter is the Python module to create GUI applications.
Itertools: Itertools provide functions for efficient looping.
from itertools import cycle
import tkinter as tk
cycle is an iterator from itertools module. It will return each image one by one out of the images passed.
Step 2: Create the App:
Instead of writing functions, we will use object oriented approach here for convenience and readability.
We’ll create a class for the app and there be defining:
1. constructor: specifying the geometry of the window, creating labels for displaying the images, and forming an iterator for them.
2. a function to display the slideshow.
3. a function specifying the Tkinter infinite loop so that, the app runs till the user hit the close button.
Let’s design
class App(tk.Tk):
'''constructor to initialize and define'''
def __init__(self,image_files,x,y,delay):
#form a Tkinter window
tk.Tk.__init__(self)
#assign customized geometry
self.geometry(f'{x}x{y}')
#assign custom time between two images
self.delay = delay
#create iterator for picture
self.pictures = cycle((tk.PhotoImage(file=image),image) for image in image_files)
#create lable to display pictures
self.pictures_display = tk.Label(self)
self.pictures_display.pack()
'''function to display the slides'''
def show_slides(self):
#display next item in iterator
image_object, image_name = next(self.pictures)
#display the images with title after specified time
self.pictures_display.config(image=image_object)
self.title(image_name)
self.after(self.delay,self.show_slides)
'''function to run the window'''
def run(self):
self.mainloop()
As discussed earlier, the constructor and functions are designed to perform corresponding tasks.
Note:
PhotoImage class is used to display images in labels, canvases, buttons, and text widgets.
next() is used to return the next item in an iterator.
after() is a tkinter widgets method, that calls specified functions after given intervals of time.
Step 3: Call the class and run the application.
Here, specify customized dimensions and image path. To avoid writing the full path of the images, make sure to have the images in the same folder as your .py script.
'''main function'''
delay = 1000 #time between two images in seconds
#image files name
image_files = [
'1.png',
'2.png',
'3.png',
'4.png',
'5.png']
#dimensions
x = 600
y = 600
#call the App
app = App(image_files,x,y,delay)
app.show_slides()
app.run()
Here, I have used 5 different images for the slideshow.
Specify the time between two images in seconds.
Let’s run the app:
Complete Source code:
from itertools import cycle
import tkinter as tk
class App(tk.Tk):
'''constructor to initialize and define'''
def __init__(self,image_files,x,y,delay):
#form a Tkinter window
tk.Tk.__init__(self)
#assign customized geometry
self.geometry(f'{x}x{y}')
#assign custom time between two images
self.delay = delay
#create iterator for picture
self.pictures = cycle((tk.PhotoImage(file=image),image) for image in image_files)
#create lable to display pictures
self.pictures_display = tk.Label(self)
self.pictures_display.pack()
'''function to display the slides'''
def show_slides(self):
#display next item in iterator
image_object, image_name = next(self.pictures)
#display the images with title after specified time
self.pictures_display.config(image=image_object)
self.title(image_name)
self.after(self.delay,self.show_slides)
'''function to run the window'''
def run(self):
self.mainloop()
'''main function'''
delay = 1000 #time between two images
#image files name
image_files = [
'1.png',
'2.png',
'3.png',
'4.png',
'5.png']
#dimensions
x = 600
y = 600
#call the App
app = App(image_files,x,y,delay)
app.show_slides()
app.run()
You can customize it further for eg. you can add the number of images from your gallery or some information. You can change the delay time and even this mini project can be expanded into a big one by creating an interface for the art gallery. Add buttons and write the functions which display such sliders of different kinds.
Thank-you.
Also Read:
- People are becoming AI Engineer with this free course in 2025: Here is how to join this…
- Apply to Google’s Student Training in Engineering Program (STEP) Intern, 2025
- Self-Driving Car Saves Falling Pedestrian, Showcases Promise of Autonomous Technology
- Instant Karma: Employer Fires Tech Team with AI, Faces Backlash on LinkedIn While Seeking New Developers
- LinkedIn’s COO Reveals the AI Interview Question That Could Land You the Job in 2025
- Elon Musk’s xAI Raises $6 Billion, Valued at $45 Billion
- Google Unveils Veo 2 and Imagen 3: A New Era of AI-Generated Content
- Imagination to Reality, Unlocking the Future: Genesis Physics Engine for 4D Simulation
- Simple Code to compare Speed of Python, Java, and C++?
- Falling Stars Animation on Python.Hub October 2024
- Most Underrated Database Trick | Life-Saving SQL Command
- Python List Methods
- Top 5 Free HTML Resume Templates in 2024 | With Source Code
- How to See Connected Wi-Fi Passwords in Windows?
- 2023 Merry Christmas using Python Turtle
- 23 AI Tools You Won’t Believe are Free
- Python 3.12.1 is Now Available
- Best Deepfake Apps and Websites You Can Try for Fun
- Amazon launched free Prompt Engineering course: Enroll Now
- 10 GitHub Repositories to Master Machine Learning
- Hello World in 35 Programming Languages
- How to Scrape Data From Any Website with Python?
- Become Job Ready With Free Harvard Computer Science course: Enroll Now
- Free Python Certification course from Alison: Good for Resume
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- Udacity Giving Free Python Course: Here is how to Enroll
- Love Babbar’s Income Revealed
- Top 5 Websites to Learn Programming in 2024
- Python Internship for college students and freshers: Apply Here
- Microsoft Giving Free Python Course in 2023: Enroll Now