Tkinter Image Slide Show (Python)

vegaseat 4 Tallied Votes 19K Views Share

Shows how to create a basic slide show with the Python Tkinter GUI toolkit.

Gribouillis commented: good tkinter example +14
''' tk_image_slideshow3.py
create a Tkinter image repeating slide show
tested with Python27/33  by  vegaseat  03dec2013
'''

from itertools import cycle

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

class App(tk.Tk):
    '''Tk window/label adjusts to size of image'''
    def __init__(self, image_files, x, y, delay):
        # the root will be self
        tk.Tk.__init__(self)
        # set x, y position only
        self.geometry('+{}+{}'.format(x, y))
        self.delay = delay
        # allows repeat cycling through the pictures
        # store as (img_object, img_name) tuple
        self.pictures = cycle((tk.PhotoImage(file=image), image)
                              for image in image_files)
        self.picture_display = tk.Label(self)
        self.picture_display.pack()

    def show_slides(self):
        '''cycle through the images and show them'''
        # next works with Python26 or higher
        img_object, img_name = next(self.pictures)
        self.picture_display.config(image=img_object)
        # shows the image filename, but could be expanded
        # to show an associated description of the image
        self.title(img_name)
        self.after(self.delay, self.show_slides)

    def run(self):
        self.mainloop()


# set milliseconds time between slides
delay = 3500

# get a series of gif images you have in the working folder
# or use full path, or set directory to where the images are
image_files = [
'Slide_Farm.gif',
'Slide_House.gif',
'Slide_Sunset.gif',
'Slide_Pond.gif',
'Slide_Python.gif'
]

# upper left corner coordinates of app window
x = 100
y = 50

app = App(image_files, x, y, delay)
app.show_slides()
app.run()
sneekula 969 Nearly a Posting Maven

Works nicely!

Gribouillis 1,391 Programming Explorer Team Colleague

Please upload the gifs !

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

For some odd reason I don't remember how to upload gifs on DaniWeb.
Any help?

Gribouillis 1,391 Programming Explorer Team Colleague

In the editor, click on the paper clip, then browse your files. You may need to zip a folder with the gifs :)

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Thanks a lot, here we go ...

Gribouillis 1,391 Programming Explorer Team Colleague

Very nice, it works very well.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

It is my understanding that you use Linux or the Apple OS X?

Gribouillis 1,391 Programming Explorer Team Colleague

Linux mint 15 Olivia 64 bits KDE.

Kriston 0 Newbie Poster

Hello i get the error message. When i try to use all gifs in a folder. How do i correct this issue ?

C:\Python27\python.exe C:/Users/Klova/PycharmProjects/untitled/Screen.py
Traceback (most recent call last):
File "C:/Users/Klova/PycharmProjects/untitled/Screen.py", line 59, in <module>

C:\Python27\python.exe C:/Users/Klova/PycharmProjects/untitled/Screen.py
Traceback (most recent call last):
  File "C:/Users/Klova/PycharmProjects/untitled/Screen.py", line 59, in <module>
    app.show_slides()
  File "C:/Users/Klova/PycharmProjects/untitled/Screen.py", line 33, in show_slides
    img_object, img_name = next(self.pictures)
  File "C:/Users/Klova/PycharmProjects/untitled/Screen.py", line 26, in <genexpr>
    for image in image_files)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 3362, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 3316, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "C:\Users\Klova\Desktop\Snapfie": permission denied
HiHe 174 Junior Poster

The line
for image in image_files)
is in your error code but not in the original snippet.
So, if you doctored up the snippet code, we would have to be mind readers to help you.

micksulley 0 Newbie Poster

Can you tell me how to modify this to display jpg rather than gif pictures please?
Thanks
Mick

Gribouillis 1,391 Programming Explorer Team Colleague

@micksulley This snippet https://www.daniweb.com/programming/software-development/code/467528/show-internet-image-with-tkinter shows how to open a jpeg image with PIL in order to display it with tkinter. I think it should work for you.

micksulley 0 Newbie Poster

Many thanks, I will give that a try.

micksulley 0 Newbie Poster

Sorry but I cannot get this to work for jpg. I assume that I need to change line 25, 30 but cannot figure out how to do it. I have tried many different things an none have worked.
I think my biggest problem is that I don't really understand those lines. I see what cycle does normally but in this case it seems to be returning two values tk.PhotoImage(file=image) and image, is that the case? I expected it to just return a file name, have I misunderstood what is happening?

Many thanks
Mick

Gribouillis 1,391 Programming Explorer Team Colleague

You can add

from PIL import Image, ImageTk
import io

at the top, then add a method in class App:

def photo_image(self, jpg_filename):
    with io.open(jpg_filename, 'rb') as ifh:
        pil_image = Image.open(ifh)
        return ImageTk.PhotoImage(pil_image)

Then at line 25, replace tk.PhotoImage(file=image) with self.photo_image(image).

Notice that this code keeps all the images in memory at the same time while running the slideshow.

micksulley 0 Newbie Poster

Perfect!
Yes I guess memory may be the next problem, I will investigate.
Thanks for your help.
Mick

Patrick_10 0 Newbie Poster

Hey! Cool script! I got a few questions though. First off, how do you make the script stop after the last picture in the picture list? Secondly, do you know how to make the pictures display at say half the resolution?

Swapnil_3 0 Newbie Poster

guys, i want the code to automatically scan all the images in the folder and display them one by one in slideshow manner...please help

how to add the path of the folder instead of adding images path one by one in the code

Daniele_2 0 Newbie Poster

very nice code and also very useful!
one question, i don't understand very well how line 19 works ("tk.Tk.init(self)"), can someone explain it to me?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.