如何引导python tkinter依次显示2张图像,但在显示之间会有延迟?

时间:2019-02-08 14:00:45

标签: python tkinter

我想使用Tkinter在python中显示两个图像,但有些延迟,但是我不能。我使用以下代码:

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)

为什么不起作用?

1 个答案:

答案 0 :(得分:-1)

我不确定这是否确定是您的问题,但是如果您尚未导入模块,则list_of_floats=[0.12, 0.23, 0.30, 0.21] def save(path,l): with open(path,'w') as file: file.write(' '.join(map(str,l))) def load(path): with open(path,'r') as file: return list(map(float,file.read().split())) save('file.txt',list_of_floats) new_list=load('file.txt') print(sum(new_list)/len(new_list)) 函数将不会被识别。您可以通过放置sleep()来导入它。另外,如果您使用的是Python 3或未在Windows操作系统上运行程序,那么将不支持from time import sleep库。