【问题标题】:FileNotFoundError In ImageTkImageTk 中的 FileNotFoundError
【发布时间】:2021-12-29 04:07:07
【问题描述】:

我在 tkinter 中创建幻灯片程序

我的代码:

    folder_path=fd.askdirectory(initialdir=os.getcwd(),title="Open Directory")
    for i in os.listdir(folder_path):
        if i.endswith(".jpg") or i.endswith(".png") or i.endswith(".jpeg"):
            new_image=Image.open(i)
            new_image=ImageTk.PhotoImage(image=new_image)
            canvas.image=new_image
            canvas.create_image(0,0,image=new_image,anchor=tk.NW)
            time.sleep(5)

当我运行它时,我得到 FileNotFoundError:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\speed\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "c:\Users\speed\Documents\Text Editor In PyQt5\wmi_test.py", line 13, in open_file
    new_image=Image.open(i)
  File "C:\Users\speed\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 2968, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '2021-09-04_13.png'

【问题讨论】:

  • 您需要加入folder_path 和文件名以获得完整的路径名并使用它。

标签: python image tkinter


【解决方案1】:

您需要通过添加文件路径来更新这一行:

 new_image=Image.open(folder_path+"\"+i)

【讨论】:

  • 它有效! new_image=Image.open(folder_path+"//"+i)
  • 最好使用os.path.join(),因为它满足跨平台的文件夹分隔符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2022-01-15
  • 2012-06-19
  • 2020-07-26
  • 2021-08-27
  • 1970-01-01
相关资源
最近更新 更多