python - 如何在 python3.6 中使用 ImageTk 和 Tkinter 修复 AttributeError?
问题描述
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中做一个项目。我该如何解决这个问题?
解决方案
line 35, init self.bg = ImageTk.PhotoImage(file="unnamed.png")
文件在哪里unnanmed.png
?
利用 :
self.bg= ImageTk.PhotoImage(Image.open(file_path))
file_path
如果文件不在根目录中,则应该是文件的完整路径或相对路径。
推荐阅读
- python - Scrapy Crawler 不关注链接
- cron - Gitlab 备份 - 每天多次
- templates - 在 TYPO3 中使用不同的页面模板
- mysql - 使用 zcat 日志时间脚本导入 MySQL 完成
- java - 构造函数是否同步直到完全完成?
- html - 为什么设置为使用 CSS 变量继承的显示属性不起作用?
- python - R to Python:将一列(在数据框中)分成数据框中的几列
- shell - Makefile 如何在删除所有其他目标文件和可执行文件时不删除一个特定的目标文件?
- python - 将字符串的某些部分转换为 int python 的问题
- io - 从文件读取期间的 Fortran 运行时错误