1

i am trying to make an image slide show but i cant get the ImageTk module to work

import tkinter as tk
from itertools import cycle
from ImageTk import PhotoImage

images = ["ffa1c83.jpg", "ffa4600.jpg", "faa4149.jpg", "f099e64.png"]
photos = cycle(PhotoImage(file=image) for image in images)

def slideShow():
  img = next(photos)
  displayCanvas.config(image=img)
  root.after(50, slideShow) # 0.05 seconds

root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenwidth()
root.geometry('%dx%d' % (640, 480))
displayCanvas = tk.Label(root)
displayCanvas.pack()
root.after(10, lambda: slideShow())
root.mainloop()

i am using python 3.7

i get this error:

Traceback (most recent call last):
  File "C:\Users\cj\Desktop\code_stuff\slideshow\slide show V2\SlideShowV2.py", 
line 3, in <module>
    from ImageTk import PhotoImage
ModuleNotFoundError: No module named 'ImageTk'
anytarsier67
  • 33
  • 1
  • 11
  • `ImageTk` is part of module `PIL`/`pillow` - use `from PIL import ImageTk, Image` – furas Dec 02 '19 at 04:33
  • @furas i tried that and got the exact same error code – anytarsier67 Dec 02 '19 at 04:35
  • did you remove `from ImageTk import PhotoImage` ? – furas Dec 02 '19 at 04:36
  • you have to remove `from ImageTk import PhotoImage` and use `from PIL import ImageTk, Image` and then you will have to use `ImageTk.PhotoImage(Image.open(filename))` . – furas Dec 02 '19 at 04:37
  • because you have to use `ImageTk.PhotoImage(Image.open(filename))` – furas Dec 02 '19 at 04:39
  • @furas where do i put `ImageTk.PhotoImage(Image.open(filename))` ? i am very new to this kind of thing. – anytarsier67 Dec 02 '19 at 04:41
  • you put it instead of `PhotoImage(file=image)` – furas Dec 02 '19 at 04:42
  • BTW: in `after()` you have to use function's name without `()` - `after(10, slideShow)` - it is so called "callback" and you use it also in `tk.Button(comman=callback)` and `widget.bind(event, callback, arg)` – furas Dec 02 '19 at 04:46
  • i got a very large error here is a screen shot since i cant paste the whole thing : https://ibb.co/BBGzPFr also: i dont understand what you mean by that – anytarsier67 Dec 02 '19 at 04:48
  • you used wrong variable. it should be `image` instead of `images` in `Image.open(image)` – furas Dec 02 '19 at 04:50
  • its working now, thanks for the help and being patient with my minimal knowledge! – anytarsier67 Dec 02 '19 at 04:52
  • and read about bug in PhotoImage - it is common problem when you load image but it doesn't show in window. – furas Dec 02 '19 at 04:53

1 Answers1

1

ImageTk is part of module PIL/pillow

from PIL import ImageTk

ImageTk.PhotoImage is used instead of standard tkinter.PhotoImage which can't read .png.

In older versions tkinter.PhotoImage couldn't read .jpg and it was working only with .gif.


Minimal code

import tkinter as tk
from itertools import cycle
from PIL import ImageTk

images = ["ffa1c83.jpg", "ffa4600.jpg", "faa4149.jpg", "f099e64.png"]
photos = cycle(ImageTk.PhotoImage(file=image) for image in images)

BTW: Read about bug in PhotoImage which removes image from memory when it is assigned to local variable in function - see Note in doc PhotoImage

furas
  • 134,197
  • 12
  • 106
  • 148