[使用tkinter和ImageTk的幻灯片显示

问题描述 投票:0回答:1

我试图制作图像幻灯片,但无法使ImageTk模块正常工作

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

我正在使用python 3.7

我收到此错误:

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'
python python-3.x image tkinter python-3.7
1个回答
1
投票

[ImageTk是模块PIL / pillow]的一部分>

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

images = ["ffa1c83.jpg", "ffa4600.jpg", "faa4149.jpg", "f099e64.png"]
photos = cycle(ImageTk.PhotoImage(Image.open(image)) for image in images)
© www.soinside.com 2019 - 2024. All rights reserved.