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:
- Unveiling the Future of AI Detector
- CodeWithHarry Earns 20 Lakhs per month from YouTube?
- Cleaning Service Booking System in Python Tkinter
- Farmers Ecommerce App using Python Tkinter
- Guidelines for Project Collaboration Process
- The system of the binary conversion
- What is web development for beginners?
- Guide to Proxy Servers: How They Work and Why You Need Them?
- Python | Check Armstrong Number using for loop
- Python | Factorial of a number using for loop
- Link in bio
- Microsoft Giving Free Machine Learning Course: Enroll Now
- Accenture Giving Free Developer Certificate in 2023
- Python | Asking the user for input until they give a valid response
- Python | How to iterate through two lists in parallel?
- Amazon Summer Internship 2023
- Python | How to sort a dictionary by value?
- Amazon Giving Free Machine Learning Course with Certificate: Enroll Now
- Google Summer Internship 2023
- Python | How to split a list into equally-sized chunks?
- 5 Secret ChatGPT skills to make money
- Python | Remove items from a list while iterating
- Free Google Certification Courses
- 5 AI tools for coders better than ChatGPT
- Python | How to get dictionary keys as a list
- New secrets to Earn money with Python in 2023
- Flower classification using CNN
- How to represent Enum in Python?
- 5 methods | Flatten a list of lists | Convert nested list into a single list in Python
- What does if __name__ == __main__ do in Python?