I know that this has been answered already, but my situation is slightly different, and in that question, I found no answers.
Original Code:
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()
My Original Console:
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 %
Code that causes the problem:
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)
If I make a new file and import the correct libraries and canvas and such, then it works just fine!
Some People are saying that these lines of code are causing the problem:
img = ImageTk.PhotoImage(file='Blackvariant-Button-Ui-System-Apps-Chess.png')
win.iconphoto(True, img)
But they're not. If I remove these lines of code:
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)
That image loads and imports perfectly!
Ok, if I do remove those lines of code, I get a FileNotFound error, which is fine and I fix it. It now runs... but the Image doesn't load!!!!!
If I make a new file and import the correct libraries and canvas and such, then it works just fine!
Current Code:
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()
Current Console:
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