首页 > 解决方案 > Python IDLE 找不到 .PNG 文件:“没有这样的文件或目录”

问题描述

我遇到了 python idle 找不到我的图像的问题,我将它保存在桌面中为'bg.png'. python将其显示为未找到问题的错误。

发现的错误是:

    Traceback (most recent call last):
      File "C:\Users\telta\Desktop\all stuff\stickman game.py", line 233, in    
<module>
    g=Game()
      File "C:\Users\telta\Desktop\all stuff\stickman game.py", line 17, in     __init__
        self.bg = PhotoImage(file="bg.png")
      File "C:\Users\telta\AppData\Local\Programs\Python\Python37-    32\lib\tkinter\__init__.py", line 3545, in __init__
        Image.__init__(self, 'photo', name, cnf, master, **kw)
      File "C:\Users\telta\AppData\Local\Programs\Python\Python37-    32\lib\tkinter\__init__.py", line 3501, in __init__
        self.tk.call(('image', 'create', imgtype, name,) + options)
    _tkinter.TclError: couldn't open "bg.png": no such file or directory

标签: pythontkinter

解决方案


改变

self.bg = PhotoImage(file="bg.png")

self.bg = PhotoImage(file="C:\complete-path-to-file\bg.png")

将“complete-path-to-file”替换为实际路径!


推荐阅读