Image Slider in Tkinter: Mini Python project for Beginners

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:

Share:

Author: Ayush Purawr