''' 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()
8
Contributors
18
Replies
159
Views
3 Years
Discussion Span
Last Post by Swapnil_3
0

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

Edited by vegaseat

0

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
0

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

0

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

0

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

0

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

0

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

0

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?

0

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

Edited by Swapnil_3

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