Python 3.x 覆盆子皮3b+;幻灯片放映程序不工作
我想把我的raspberry pi变成一个幻灯片设备,但我几乎不知道python 3。有人能帮我修复我在网上找到的这段代码吗?它应该显示我创建的列表中的图像,但它没有运行。我对这段代码所做的唯一更改就是创建了列表,所以我不知道为什么它不起作用。我得到的错误是Python 3.x 覆盆子皮3b+;幻灯片放映程序不工作,python-3.x,raspberry-pi3,Python 3.x,Raspberry Pi3,我想把我的raspberry pi变成一个幻灯片设备,但我几乎不知道python 3。有人能帮我修复我在网上找到的这段代码吗?它应该显示我创建的列表中的图像,但它没有运行。我对这段代码所做的唯一更改就是创建了列表,所以我不知道为什么它不起作用。我得到的错误是 Traceback (most recent call last): File "bro.py", line 82, in <module> slideshow.start() File "bro.py", li
Traceback (most recent call last):
File "bro.py", line 82, in <module>
slideshow.start()
File "bro.py", line 56, in start
self.main()
File "bro.py", line 48, in main
self.set_image()
File "bro.py", line 42, in set_image
self.image_name = next(self.images)
StopIteration
尝试next(iter(images))
而不是next(images)
顺便说一句,我在我的速子实验室GitHub上为Tkinter和Kivy制作了Raspberry Pi幻灯片。。。他们也会从Instagram下载照片,但如果你不连接,或者没有互联网连接,他们只会按照你选择的顺序和持续时间,在你指定的图像目录中循环浏览图像
#!/usr/bin/env python3
"""Display a slideshow from a list of filenames"""
import os
import tkinter
from itertools import cycle
from PIL import Image, ImageTk
class Slideshow(tkinter.Tk):
"""Display a slideshow from a list of filenames"""
def __init__(self, images, slide_interval):
"""Initialize
images = a list of filename
slide_interval = milliseconds to display image
"""
tkinter.Tk.__init__(self)
self.geometry("+0+0")
self.slide_interval = slide_interval
self.images = images
self.set_images(images)
self.slide = tkinter.Label(self)
self.slide.pack()
def set_images(self, images):
self.images = cycle(images)
def center(self):
"""Center the slide window on the screen"""
self.update_idletasks()
w = self.winfo_screenwidth()
h = self.winfo_screenheight()
size = tuple(int(_) for _ in self.geometry().split('+')[0].split('x'))
x = w / 2 - size[0] / 2
y = h / 2 - size[1] / 2
self.geometry("+%d+%d" % (x, y))
def set_image(self):
"""Setup image to be displayed"""
self.image_name = next(self.images)
filename, ext = os.path.splitext(self.image_name)
self.image = ImageTk.PhotoImage(Image.open(self.image_name))
def main(self):
"""Display the images"""
self.set_image()
self.slide.config(image=self.image)
self.title(self.image_name)
self.center()
self.after(self.slide_interval, self.start)
def start(self):
"""Start method"""
self.main()
self.mainloop()
if __name__ == "__main__":
slide_interval = 2500
# use a list
images = [
"/~/definitely/1550099164562.png",
"/~/definitely/1550770995551.png",
"/~/definitely/1550771217013.png",
"/~/definitely/1550771726391.jpg"]
# all the specified file types in a directory
# "." us the directory the script is in.
# exts is the file extentions to use. it can be any extention that pillow supports
# http://pillow.readthedocs.io/en/3.3.x/handbook/image-file-formats.html
import glob
images = glob.glob("*.jpg")
path = "."
exts = ["jpg", "bmp", "png", "gif", "jpeg"]
images = [fn for fn in os.listdir(path) if any(fn.endswith(ext) for ext in exts)]
# start the slideshow
slideshow = Slideshow(images, slide_interval)
slideshow.start()