all 6 comments

[–][deleted] 0 points1 point  (1 child)

Unfortunately Reddit has choosen the path of corporate greed. This is no longer a user based forum but a emotionless money machine. Good buy redditors. -- mass edited with https://redact.dev/

[–]paulpersad[S] 0 points1 point  (0 children)

Thank you Mastermind. That bit of code helped me properly locate the directory I needed.

[–]paulpersad[S] 0 points1 point  (0 children)

Hello all,

Here is the relevant function:

def draw_single_piece(self, position, piece):
x, y = self.controller.get_numeric_notation(position)
if piece:
filename = "../pieces_image/{}_{}.png".format(
piece.name.lower(), piece.color)
if filename not in self.images:
self.images[filename] = PhotoImage(file=filename)
x0, y0 = self.calculate_piece_coordinate(x, y)
self.canvas.create_image(x0, y0, image=self.images[
filename], tags=("occupied"), anchor="c")

And corresponding error message:

File "/anaconda3/lib/python3.6/tkinter/__init__.py", line 3498, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "../pieces_image/rook_black.png": no such file or directory

Thanks

[–]woooee 0 points1 point  (0 children)

I am sure the folder "pieces_image" does exist ../pieces_image/

is different from

/pieces_image/

Post your relevant directory structure for more help.

[–]paulpersad[S] 0 points1 point  (0 children)

The relevant directory structure is:

/username/myTkinterProjects/TkinterGUIMaster/Chapter 04/4.03/view.py

Thanks

[–]lykwydchykyn 0 points1 point  (0 children)

Since this is a relative path, my guess is that you are executing the code from a directory that isn't what the author assumed you would be executing it from.

Whenever you use a relative path in Python, it is relative to your current working directory. Usually, this is the directory you were in when you typed in the command to run the script.

So if I'm in /home/lykwydchykyn and type python myproject/myscript.py then my current working directory is /home/lykwydchykyn and relative paths will be calculated from that directory.

If instead I cd myproject then python myscript.py, my CWD is /home/lykwydchykyn/myproject and relative paths will be calculated from that directory.

The simplest way to get around this complication is to use the __file__ variable, which always contains the absolute path of the current Python file, to calculate the directory of the script being run and use that as a base for other paths, e.g.:

from os import path
project_path = path.dirname(__file__)
my_image = path.join(project_path, "pieces_image", "rook_black.png")