''' tk_image_slideshow3.py
create a Tkinter image repeating slide show
tested with Python27/33  by  vegaseat  03dec2013

from itertools import cycle

    # 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
        # 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)

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

    def run(self):

# 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 = [

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

app = App(image_files, x, y, delay)
3 Years
Discussion Span
Last Post by Patrick_10

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

Edited by vegaseat


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>
  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

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.

Edited by HiHe


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


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


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.

Edited by Gribouillis


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


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?

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.