簡體   English   中英

Tkinter PhotoImage 不存在

[英]Tkinter PhotoImage doesn't exist

當我偶然發現一個錯誤時,我試圖讓我的應用程序的運動原型工作,這是與 Tkinter 的 PhotoImage。

起初,我決定使用 Pathlib,但我開始相信這是一個錯誤(我仍然需要一種方法讓非 Windows 用戶使用此應用程序)。

## -- 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) 

運行此程序時出現以下錯誤

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

你寫的代碼沒有問題。 Python 找不到該文件。 可能是你運行 Python 的目錄和代碼不同的目錄。 如果在系統變量中加入Python ,問題就解決了。

此外,不要使用雙斜杠,您可以按如下方式編寫代碼更實用。

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

還要確保您的 python 代碼在資產目錄中。

YourProjectFolder/
├──YourCode.py
└── Assets/
    └── Char/
        └── Player/
            └── Untitled.png

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM