-1

I was trying to get the movement prototype for my application working when I stumbled across an error, this was with Tkinter's PhotoImage.

At first, I decided that I would use Pathlib but I am starting to believe that is a mistake (I do still need a way for non-windows users to use this application).

## -- Setup -- ##
# Imports and Variables
from pathlib import Path # Increases accessibility on mac and Linux machines
from tkinter import *

window = Tk()
window.title("Project 001 - Pre-Alpha 0.1 (pa0.1)")

Game = Canvas(window, width = 1280, height = 720)
Game.config(bg = "white")
Game.pack()

# Defining Images
PlayerImgPath = Path("Assets\\Char\\Player\\Untitled.png") # Player
PlayerImg = PhotoImage(file = PlayerImgPath)

# Sprites
Player = Game.create_image(20, 250, image = PlayerImg) 

When running this I get the following error

file "D:\PythonD\lib\tkinter\__init__.py", line 3495, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "Assets\Char\Player\Untitled.png": no such file or directory
NaNdy
  • 99
  • 8
  • 1
    This isn't a tkinter problem. You're simply giving the wrong path to a function that expects a path to an existing file. – Bryan Oakley Jan 21 '20 at 16:46
  • @tgikal: relative paths are relative to the current working directory, not relative to the folder where the script resides. – Bryan Oakley Jan 21 '20 at 16:47
  • 1
    If you need non-windows users to be able to use the program, use forward slashes. They work on all platforms, including windows. – Bryan Oakley Jan 21 '20 at 16:48
  • @tgikal: that is absolutely not true at all. They are only the same if you cd to the location of the script. It's very, very common to place scripts in a separate folder from the current working directory. – Bryan Oakley Jan 21 '20 at 16:50
  • If you want to construct a path relative to the directory the script itself is in, you can use the script's built-in `__file__` attribute to determine its location (including its parent folder(s)). – martineau Jan 21 '20 at 16:58
  • @BryanOakley It is a correct path, in VS I used the "copy path" button to put it into the code. – NaNdy Jan 21 '20 at 17:43
  • It cannot be a correct path. Python file functions have been tested millions of times. If it says it can't find a file, the file is almost certainly not there. Are you aware that relative paths are relative to your current directory and not the directory of the script? That path might be correct in the context of VSCode, but not valid when running the program from a command line or when double-clicking on its icon. – Bryan Oakley Jan 21 '20 at 18:13
  • Try to print out the absolute path `PlayerImgPath.resolve()`. – acw1668 Jan 22 '20 at 11:25
  • @BryanOakley Yes I am, it is just frustrating when my file structure (which I have now moved) looks like this ProgrammingFolder/OwlsToolbox/Projects/Project001/Assets/Player Project001 is where the script is stored and Player is where the photo is stored – NaNdy Jan 22 '20 at 17:04
  • 1
    What is the directory you are in when you run the script? Perhaps immediately before trying to access the file you can have the script print out the results of `os.getcwd()`. – Bryan Oakley Jan 22 '20 at 17:50
  • @BryanOakley Seems that has sorted out the issue, thanks for the help. – NaNdy Jan 23 '20 at 18:10

1 Answers1

1

There is no problem in the code you wrote. Python cannot find the file. Probably the directory where you run Python and the directory where the codes are different. If you add Python to system variables, the problem will be solved.

Besides, instead of using double slash, it will be more practical for you to write your code as follows.

PlayerImgPath = Path(r"Assets/Char/Player/Untitled.png")

Also make sure your python code is up the assets directory.

YourProjectFolder/
├──YourCode.py
└── Assets/
    └── Char/
        └── Player/
            └── Untitled.png
  • 1
    Realistically `"Assets\\Char\\Player\\Untitled.png"` can be written `"Assets/Char/Player/Untitled.png"` and work just as well in every os. But yeah, this has nothing to do with Tkinter Photoimage. – tgikal Jan 21 '20 at 16:42
  • That is exactly how my file structure is like, the full directory is D:\Programming Folder\Assets\Char\Player\Untitled.png – NaNdy Jan 21 '20 at 17:07
  • Are you running your code via cmd or a IDE? If you are running via cmd. I recommend adding python to system variables. – Muhammet Emin TURGUT Jan 21 '20 at 17:34
  • @MuhammetEminTURGUT I use Visual Studio Code to run it, sometimes I will use IDLE. – NaNdy Jan 21 '20 at 23:45