python - 枕头属性错误:“PhotoImage”对象没有属性“_PhotoImage__photo”
问题描述
我知道这已经被回答了,但我的情况略有不同,在那个问题中,我没有找到答案。
原始代码:
import Tkinter as tk
from PIL import Image, ImageTk
import pathlib
win = tk.Tk()
win.title('Chess')
img = ImageTk.PhotoImage(file='Blackvariant-Button-Ui-System-Apps-Chess.png')
win.iconphoto(True, img)
#this class controls the viewing of the game and calculating the legal moves. eg if a pawn on e2 can move to e5 and drawing the board
class Game:
#start of the Game class
#init
def __init__(self,root):
print('The class has been declared')
#creates the canvas
self.base = tk.Canvas(win,bg='#f0d9b5',width='400',height='400')
#function to draw the base chess board(the squares)
def drawBoard(self):
#variable declarations
i = 1
x = 0
y = 0
#test
self.base.create_rectangle(0,0,100,100,fill='#b58862',)
#create crucial isWhite variable
isWhite = True
#board drawing loop
while i < 65:
i += 1
#if statement draws all the brown squares on the board
if isWhite == False:
self.base.create_rectangle(x,y,x+100,y+100,fill='#b58862',outline = '#b58862')
#debug
print('drew a brown square at coordinates x =', x ,'y =', y)
#some logic to know how and where to draw the next square
if x == 350:
y += 50
x = 0
continue
else:
x += 50
isWhite = True
continue
#if statement draws all the white squares on the board
if isWhite:
self.base.create_rectangle(x,y,x+100,y+100,fill='#f0d9b5', outline='#f0d9b5')
#debug
print('drew a white square at coordinates x =', x ,'y =', y)
#some logic to know how and where to draw the next square
if x == 350:
y += 50
x = 0
continue
else:
x += 50
isWhite = False
continue
#packs canvas
self.base.pack()
#end of drawBoard()
#this class draws the pieces from an array of the pieces' locations
def drawPieces(self):
path = str(pathlib.Path(__file__).parent.absolute()) + '/Chess/Sprites/W_K.png'
norm_img = Image.open(path)
img = norm_img.resize((50,50))
photo = ImageTk.PhotoImage(image=img)
king = self.base.create_image((100,100),image=photo)
#end of the Game class
#creates new instance of the Game class
game = Game(win)
#runs the drawBoard function
game.drawBoard()
#runs the drawPieces function
game.drawPieces()
#main loop
win.mainloop()
我的原始控制台:
munmun@Munmuns-Air Coding % /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 "/Users/munmun/Desktop/Babai's Folder/Coding/Python/Projects/Chess/Chess AI.py"
Traceback (most recent call last):
File "/Users/munmun/Desktop/Babai's Folder/Coding/Python/Projects/Chess/Chess AI.py", line 7, in <module>
img = ImageTk.PhotoImage(file='Blackvariant-Button-Ui-System-Apps-Chess.png')
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/ImageTk.py", line 89, in __init__
image = _get_image_from_kw(kw)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/ImageTk.py", line 58, in _get_image_from_kw
return Image.open(source)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/Image.py", line 2904, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Blackvariant-Button-Ui-System-Apps-Chess.png'
Exception ignored in: <function PhotoImage.__del__ at 0x7fee180a8040>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/PIL/ImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
munmun@Munmuns-Air Coding %
导致问题的代码:
path = str(pathlib.Path(__file__).parent.absolute()) + '/Chess/Sprites/W_K.png'
norm_img = Image.open(path)
img = norm_img.resize((50,50))
photo = ImageTk.PhotoImage(image=img)
king = self.base.create_image((100,100),image=photo)
如果我创建一个新文件并导入正确的库和画布等,那么它就可以了!
有人说这些代码行导致了问题:
img = ImageTk.PhotoImage(file='Blackvariant-Button-Ui-System-Apps-Chess.png')
win.iconphoto(True, img)
但他们不是。如果我删除这些代码行:
path = str(pathlib.Path(__file__).parent.absolute()) + '/Chess/Sprites/W_K.png'
norm_img = Image.open(path)
img = norm_img.resize((50,50))
photo = ImageTk.PhotoImage(image=img)
king = self.base.create_image((100,100),image=photo)
该图像可以完美加载和导入!
好的,如果我确实删除了这些代码行,我会收到 FileNotFound 错误,这很好,我修复了它。它现在运行......但图像不加载!!!!
如果我创建一个新文件并导入正确的库和画布等,那么它就可以了!
当前代码:
import tkinter as tk
from PIL import Image, ImageTk
import pathlib
win = tk.Tk()
win.title('Chess')
#this class controls the viewing of the game and calculating the legal moves. eg if a pawn on e2 can move to e5 and drawing the board
class Game:
#start of the Game class
#start of the Game class
#start of the Game class
#start of the Game class
#start of the Game class
#init
def __init__(self,root):
print('The class has been declared')
#creates the canvas
self.base = tk.Canvas(win,bg='#f0d9b5',width='400',height='400')
#function to draw the base chess board(the squares)
def drawBoard(self):
#variable declarations
i = 1
x = 0
y = 0
#test
self.base.create_rectangle(0,0,100,100,fill='#b58862',)
#create crucial isWhite variable
isWhite = True
#board drawing loop
while i < 65:
i += 1
#if statement draws all the brown squares on the board
if isWhite == False:
self.base.create_rectangle(x,y,x+100,y+100,fill='#b58862',outline = '#b58862')
#debug
print('drew a brown square at coordinates x =', x ,'y =', y)
#some logic to know how and where to draw the next square
if x == 350:
y += 50
x = 0
continue
else:
x += 50
isWhite = True
continue
#if statement draws all the white squares on the board
if isWhite:
self.base.create_rectangle(x,y,x+100,y+100,fill='#f0d9b5', outline='#f0d9b5')
#debug
print('drew a white square at coordinates x =', x ,'y =', y)
#some logic to know how and where to draw the next square
if x == 350:
y += 50
x = 0
continue
else:
x += 50
isWhite = False
continue
#packs canvas
self.base.pack()
#end of drawBoard()
#this class draws the pieces from an array of the pieces' locations
def drawPieces(self):
path = str(pathlib.Path(__file__).parent.absolute()) + '/Sprites/W_K.png'
print('Path:' , path)
norm_img = Image.open(path)
img = norm_img.resize((50,50))
photo = ImageTk.PhotoImage(image=img)
king = self.base.create_image((100,100),image=photo)
#end of the Game class
#end of the Game class
#end of the Game class
#end of the Game class
#end of the Game class
#end of the Game class
#creates new instance of the Game class
game = Game(win)
#runs the drawBoard function
game.drawBoard()
#runs the drawPieces function
game.drawPieces()
#main loop
win.mainloop()
当前控制台:
munmun@Munmuns-Air Chess % /usr/local/bin/python3 "/Users/munmun/Desktop/Babai's Folder/Coding/Python/Projects/Chess/Chess AI.py"
The class has been declared
drew a white square at coordinates x = 0 y = 0
drew a brown square at coordinates x = 50 y = 0
drew a white square at coordinates x = 100 y = 0
drew a brown square at coordinates x = 150 y = 0
drew a white square at coordinates x = 200 y = 0
drew a brown square at coordinates x = 250 y = 0
drew a white square at coordinates x = 300 y = 0
drew a brown square at coordinates x = 350 y = 0
drew a brown square at coordinates x = 0 y = 50
drew a white square at coordinates x = 50 y = 50
drew a brown square at coordinates x = 100 y = 50
drew a white square at coordinates x = 150 y = 50
drew a brown square at coordinates x = 200 y = 50
drew a white square at coordinates x = 250 y = 50
drew a brown square at coordinates x = 300 y = 50
drew a white square at coordinates x = 350 y = 50
drew a white square at coordinates x = 0 y = 100
drew a brown square at coordinates x = 50 y = 100
drew a white square at coordinates x = 100 y = 100
drew a brown square at coordinates x = 150 y = 100
drew a white square at coordinates x = 200 y = 100
drew a brown square at coordinates x = 250 y = 100
drew a white square at coordinates x = 300 y = 100
drew a brown square at coordinates x = 350 y = 100
drew a brown square at coordinates x = 0 y = 150
drew a white square at coordinates x = 50 y = 150
drew a brown square at coordinates x = 100 y = 150
drew a white square at coordinates x = 150 y = 150
drew a brown square at coordinates x = 200 y = 150
drew a white square at coordinates x = 250 y = 150
drew a brown square at coordinates x = 300 y = 150
drew a white square at coordinates x = 350 y = 150
drew a white square at coordinates x = 0 y = 200
drew a brown square at coordinates x = 50 y = 200
drew a white square at coordinates x = 100 y = 200
drew a brown square at coordinates x = 150 y = 200
drew a white square at coordinates x = 200 y = 200
drew a brown square at coordinates x = 250 y = 200
drew a white square at coordinates x = 300 y = 200
drew a brown square at coordinates x = 350 y = 200
drew a brown square at coordinates x = 0 y = 250
drew a white square at coordinates x = 50 y = 250
drew a brown square at coordinates x = 100 y = 250
drew a white square at coordinates x = 150 y = 250
drew a brown square at coordinates x = 200 y = 250
drew a white square at coordinates x = 250 y = 250
drew a brown square at coordinates x = 300 y = 250
drew a white square at coordinates x = 350 y = 250
drew a white square at coordinates x = 0 y = 300
drew a brown square at coordinates x = 50 y = 300
drew a white square at coordinates x = 100 y = 300
drew a brown square at coordinates x = 150 y = 300
drew a white square at coordinates x = 200 y = 300
drew a brown square at coordinates x = 250 y = 300
drew a white square at coordinates x = 300 y = 300
drew a brown square at coordinates x = 350 y = 300
drew a brown square at coordinates x = 0 y = 350
drew a white square at coordinates x = 50 y = 350
drew a brown square at coordinates x = 100 y = 350
drew a white square at coordinates x = 150 y = 350
drew a brown square at coordinates x = 200 y = 350
drew a white square at coordinates x = 250 y = 350
drew a brown square at coordinates x = 300 y = 350
drew a white square at coordinates x = 350 y = 350
Path: /Users/munmun/Desktop/Babai's Folder/Coding/Python/Projects/Chess/Sprites/W_K.png
解决方案
推荐阅读
- google-analytics - 谷歌分析没有收集数据
- amazon-web-services - wget 在 AWS EC2 (Bitnami) 上不可用
- java - 如何将 Json 转换为 Java bean 对象?
- java - 如果 firebase 数据库没有子值,则开始下一个活动
- javascript - 我目前对 onclick 的使用是否有更好的选择?
- django - 初始 Django 迁移时没有这样的表
- javascript - 模块 AppRegistry 未为 Expo 注册可调用模块(调用 runApplication)
- mongodb - 填充嵌套的 ObjectId
- javascript - Firebase functions.auth.UserRecord 不是函数
- javascript - 什么是 javascript “...array” 参数?