当前位置: 首页 > 知识库问答 >
问题:

在Python中全屏显示图像

秦天宇
2023-03-14

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

导入系统,如果是系统,则导入操作系统。版本信息[0]==2:
导入Tkinter Tkinter=Tkinter其他:从PIL导入图像,ImageTk导入Tkinter

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:\用户\scott\Documents\Python\Imagetest.py",第36行,在show PIL(file)文件"e:\用户\scott\Documents\Python\Imagetest.py",第27行,在show PIL Imagesprite=canvas.create_image(w/2,h/2,图像=图像)文件"C:\Program Files\Python37\lib\tkinter__init__. py",第2486行,create_image返回自身。_create('Image',args,kw)文件"C:\Program Files\Python37\lib\tkinter__init__. py",第2477行,_create*(args自身._options(cnf, kw)))_tkinter。图片"pyImage2"不存在

共有1个答案

翟俊名
2023-03-14

在四处搜索之后,我发现第一个问题是tkinter。Tk()被调用了多次,但必须只调用一次,因此我将其从showPIL函数中移出,并转移到初始化中。下一个问题是mainloop阻塞,所以我将其替换为root的组合。更新_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)
 类似资料:
  • 我的Android应用程序需要显示一个全屏位图作为背景,然后在上面使用OpenGL ES显示一些动态的3D图形(1.1或2.0--尚未决定)。背景图像是同一个应用程序中WebView组件的快照,因此它的尺寸已经完全适合屏幕。 我是OpenGL新手,但我知道显示位图的常规方法包括将其缩放成一个POT纹理(glTexImage2D)、配置矩阵、为矩形创建一些顶点并使用GLDraWarrays来显示。似

  • 飞行模式已开启。该模式会切断手表的所有无线通信。您仍可以使用手表,但无法与 Polar Flow 移动应用程序同步数据,或将其与任何无线配件一起使用。 勿打扰模式已开启。启用免打扰后,您收到通知或来电时,手表不会震动(显示屏左下方的红点仍会亮起)。背光灯激活手势也会被禁用。 启用免打扰后,您收到通知或来电时,手表不会振动(显示屏左下方的红点仍会亮起)。背光灯激活手势也会被禁用。 闹铃已设置。您可以

  • 飞行模式已开启。飞行模式可终止来自手表的所有无线通信。您仍可以使用手表,但是,不能使用 Polar Flow 移动应用同步数据,或将手表与任何无线连接配件搭配使用。 免打扰模式已开启。启用免打扰后,您收到通知或来电时,手表不会振动(显示屏左下方的红点仍会亮起)。背光灯激活手势也会被禁用。 启用免打扰后,您收到通知或来电时,手表不会振动(显示屏下方的红点仍会亮起)。背光灯激活手势也会被禁用。 闹铃已

  • 描述 (Description) 您可以根据窗口的宽度和高度创建全屏模式。 例子 (Example) 以下示例演示了在Foundation中使用full screen modal - <!doctype html> <head> <meta charset = "utf-8" /> <meta http-equiv = "x-ua-compatible" conten

  • 问题内容: 我一直在尝试并且无法在OSX系统的主显示屏上使用Java全屏模式。无论我尝试了什么,似乎都无法摆脱显示屏顶部的“苹果”菜单栏。我真的需要在整个屏幕上绘画。谁能告诉我如何摆脱菜单? 我已经附上了一个显示问题的示例类-在我的系统上,菜单仍然可见,我希望看到一个完全空白的屏幕。 问题答案: 我认为您的问题在这里: 块总是执行,所以这里发生的是,您的窗口在短时间内(如果有的话)变为全屏,然后立

  • 我想在API调用时在flutter中显示全屏加载视图。但是当我在脚手架体中添加加载小部件时,它出现在appbar和底部导航器之后。 我花了很多时间在全屏显示加载视图。另外,我希望在API调用时防止后退操作。