如何在 python3.6 中使用 ImageTk 和 Tkinter 修复 AttributeError?

时间:2021-01-18 05:41:34

标签: python tkinter

Traceback (most recent call last):
  File "C:/Users/Murad/PycharmProjects/OmrTest/images/GUI.py", line 765, in <module>
    obj = Login(master)
  File "C:/Users/Murad/PycharmProjects/OmrTest/images/GUI.py", line 35, in __init__
    self.bg = ImageTk.PhotoImage(file="unnamed.png")
  File "C:\Users\Murad\PycharmProjects\OmrTest\venv\lib\site-packages\PIL\ImageTk.py", line 89, in __init__
    image = _get_image_from_kw(kw)
  File "C:\Users\Murad\PycharmProjects\OmrTest\venv\lib\site-packages\PIL\ImageTk.py", line 58, in _get_image_from_kw
    return Image.open(source)
  File "C:\Users\Murad\PycharmProjects\OmrTest\venv\lib\site-packages\PIL\Image.py", line 2904, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'unnamed.png'
Exception ignored in: <bound method PhotoImage.__del__ of <PIL.ImageTk.PhotoImage object at 0x000002A705690748>>
Traceback (most recent call last):
  File "C:\Users\Murad\PycharmProjects\OmrTest\venv\lib\site-packages\PIL\ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

所以我在 pycharm 中做了一个项目。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

line 35, init self.bg = ImageTk.PhotoImage(file="unnamed.png")

文件 unnanmed.png 在哪里?

使用:

self.bg= ImageTk.PhotoImage(Image.open(file_path))

file_path 应该是文件的完整或相对路径,如果它不在根目录中。

相关问题