我想说明使用Tkinter的一些延迟蟒蛇两个图像,但我不能。我用这个代码:
import sys
import time
if sys.version_info[0] == 2: # the tkinter library changed it's name from Python 2 to 3.
import Tkinter
tkinter = Tkinter #I decided to use a library reference to avoid potential naming conflicts with people's programs.
else:
import tkinter
from PIL import Image, ImageTk
def showPIL(pilImage):
root = tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))
canvas = tkinter.Canvas(root,width=w,height=h)
canvas.pack()
canvas.configure(background='black')
imgWidth, imgHeight = pilImage.size
if imgWidth > w or imgHeight > h:
ratio = min(w/imgWidth, h/imgHeight)
imgWidth = int(imgWidth*ratio)
imgHeight = int(imgHeight*ratio)
pilImage = pilImage.resize((imgWidth,imgHeight), Image.ANTIALIAS)
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image(w/2,h/2,image=image)
root.mainloop()
和该功能后,用于显示图像:
pilImage = Image.open("colagem3.png")
showPIL(pilImage)
time.sleep(2)
pilImage = Image.open("colagem3.png")
showPIL(pilImage)
time.sleep(2)
为什么它不工作?
我不知道这是肯定你的问题在这里,但sleep()
功能是不会,如果你还没有导入模块,它得到认可。您可以通过将from time import sleep
导入。此外,如果你正在使用Python 3或没有运行您的Windows操作系统的程序则PIL
库是不会得到支持。