Python - Create Image Slider In Tkinter

How To Make a Python Image Slider Using Tkinter

Python - Create Image Slider In Tkinter


In this Python Tutorial we will see How to Make an Imges Viewer Using Tkinter .
To create this simple image slider application, we need to use the Tkinter library for graphical user interface (GUI) and the Python Imaging Library (PIL) for image handling.





Project Source Code:


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


# Create a Tkinter root window
root = Tk()
root.geometry('400x400')
root.title("Slider")

# List of image file paths
pics_list = ["C:/Users/1BestCsharp/Downloads/lms images/New folder/prd1.png",
"C:/Users/1BestCsharp/Downloads/lms images/New folder/prd2.png",
"C:/Users/1BestCsharp/Downloads/lms images/New folder/prd3.png",
"C:/Users/1BestCsharp/Downloads/lms images/New folder/prd4.png",
"C:/Users/1BestCsharp/Downloads/lms images/New folder/prd5.png",
"C:/Users/1BestCsharp/Downloads/lms images/New folder/prd6.png"]


# Create labels for displaying image and navigation buttons
lbl_next = tk.Label(text=">", font=("Verdana",25))
lbl_previous = tk.Label(text="<", font=("Verdana",25))
lbl_picture = tk.Label()

# Grid layout for labels
lbl_previous.grid(row=0, column=0)
lbl_picture.grid(row=0, column=1)
lbl_next.grid(row=0, column=2)

# Initialize the index to track the current image
index = 0

def nextImage():
global index, img

# Increment the index to show the next image in the list
index += 1
# If the index goes beyond the last image, loop back to the first image
if index > len(pics_list) - 1:
index = 0

# Load and display the image at the current index
img = Image.open(pics_list[index])
img = img.resize((330,400), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
lbl_picture['image'] = img



def previousImage():
global index, img

# Decrement the index to show the previous image in the list
index -= 1

# If the index goes below 0, loop back to the last image
if index < 0:
index = len(pics_list) - 1

# Load and display the image at the current index
img = Image.open(pics_list[index])
img = img.resize((330,400), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
lbl_picture['image'] = img


# Bind the navigation buttons to their respective functions
lbl_next.bind("<Button-1>", lambda abc : nextImage())
lbl_previous.bind("<Button-1>", lambda abc : previousImage())


# Show the first image in the list when the program starts
nextImage()

# Start the Tkinter event loop
root.mainloop()


                                       

////// OUTPUT : 

Python Tutorial: How to Make an Image Slider Using Tkinter

How to Make an Image Slider In Python Using Tkinter

How to Make an Image Slider In Python Using Tkinter





download the source code












Share this

Related Posts

Previous
Next Post »