找不到文件,即使目录正确

File not found, even though the directory is correct

我目前正在尝试制作游戏,我正在从 python 文件所在的同一文件夹中为按钮导入图像。方法:

dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
imagepath = os.path.join(dirname, "redvase.png")
vase1 = Button(root, relief=FLAT, background="white", image=imagepath)
vase1.place(x=330,y=240,height=30,width=30)

输出(图像路径)为C:\Users\ - - \Desktop\Pythonproject\redvase.png

我的文件就在那个确切的路径上,但我仍然收到一条错误消息,指出该文件不存在。

vase1 = Button(root, relief=FLAT, background="white", image=imagepath)
......
_tkinter.TclError: image "C:\Users\- -\Desktop\Pythonproject\redvase.png" doesn't exist

如果有帮助,我正在使用 Windows 和 Python 3.4.3.

按钮的图像属性接受图像对象的输入,而不是图像文件的路径。 为了获得图像,您可以使用 PhotoImage:

photo=PhotoImage(file="redvase.gif")
vase1 = Button(root, relief=FLAT, background="white", image=photo)