【问题标题】:displaying an image full screen in Python在 Python 中全屏显示图像
【发布时间】:2019-05-07 09:59:25
【问题描述】:

以下程序适用于目录中的第一个 .jpg。 当第二次调用它时,它会得到一个“_tkinter.TclError: image “pyimage2”不存在”异常。为什么会报错?是 有没有办法重用第一个图像而不是创建第二个?

导入系统,操作系统 如果 sys.version_info[0] == 2:
导入 Tkinter tkinter = Tkinter 别的: 导入 tkinter 从 PIL 导入图像,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
 # resize photo to full screen 
    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)
    print(image)
    imagesprite = canvas.create_image(w/2,h/2,image=image)
    root.mainloop()

names = os.listdir("E://Users//scott//Pictures")
print(names)
for file in names:
    print(file)
    if file[-4:] == ".jpg":
        file=Image.open("E://Users//scott//Pictures//"+file)
        showPIL(file)

这是控制台输出。 Traceback(最近一次通话最后一次):文件 “e:\Users\scott\Documents\Python\image test.py”,第 36 行,在 showPIL(file) 文件“e:\Users\scott\Documents\Python\image test.py”,第 27 行,在 showPIL imagesprite = canvas.create_image(w/2,h/2,image=image) 文件“C:\Program Files\Python37\lib\tkinter__init__.py”,第 2486 行,在 创建图像 return self._create('image', args, kw) 文件“C:\Program Files\Python37\lib\tkinter__init__.py”,第 2477 行,在 _create *(args + self._options(cnf, kw)))) _tkinter.TclError:图像“pyimage2”不存在

>

【问题讨论】:

    标签: python-3.x tkinter python-imaging-library


    【解决方案1】:

    四处搜索后,我发现 tkinter.Tk() 被多次调用而必须调用的第一个问题 只有一次,所以我把它从 showPIL 函数中移到了 初始化。下一个问题是 mainloop 阻塞了,所以我 将其替换为 root.update_idletasks() 和 根。更新()。以下工作符合我的期望和需要:

    import sys, os
    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
    import time
    
    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()
    canvas = tkinter.Canvas(root,width=w,height=h)
    canvas.pack()
    canvas.configure(background='black')
    
    
    def showPIL(pilImage):
        imgWidth, imgHeight = pilImage.size
     # resize photo to full screen 
        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.update_idletasks()
        root.update()
    #    root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))
    
    
    names = os.listdir("E://Users//scott//Pictures")
    print(names)
    for file in names:
    
        print(file)
        if file[-4:] == ".jpg":
            file=Image.open("E://Users//scott//Pictures//"+file)
            showPIL(file)
    
            time.sleep(5)
    

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 2010-12-16
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多