【发布时间】:2020-02-16 00:11:37
【问题描述】:
这是我的代码:
from tkinter import *
import turtle as t
import random
bob=Tk()
t.setup(width = 200,height = 200)
t.bgpic(picname="snakesandladders.gif")
def left(event):
t.begin_fill()
print("left")
t.left(90)
def right(event):
print("right")
t.right(90)
def forward(event):
print("forward")
t.forward(100)
block = Frame(bob, width=300, height=250)
bob.bind("<a>", left)
bob.bind("<d>", right)
bob.bind("<w>", forward)
block.pack()
bob.mainloop()
我的错误是:
Traceback (most recent call last):
File "/Users/lolszva/Documents/test game.py", line 6, in <module>
t.bgpic(picname="snakesandladders.gif")
File "<string>", line 8, in bgpic
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 1481, in bgpic
self._bgpics[picname] = self._image(picname)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 479, in _image
return TK.PhotoImage(file=filename)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3545, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 3501, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "snakesandladders.gif": no such file or directory
我是初学者,所以我真的不知道我是否没有导入库。在另一个bgpic() 不起作用的类似问题中,它说要将图片转换为 gif,但对我来说它仍然不起作用。我使用的是 Mac OSX Python 3.8.0 版。
【问题讨论】:
-
尝试一次指定文件的完整路径,而不是只指定文件名
-
“没有这样的文件或目录”:你从
TclError:消息中看不懂什么? -
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) _tkinter.TclError: image "pyimage2" 不存在
-
这个错误是什么意思。当我输入完整的文件名时会发生这种情况
-
@lolszvaz: ` _tkinter.TclError: image "pyimage2" doesn't exist`图像对象不在内存中存在。使用此示例验证您的图像:why-doesnt-a-local-name-with-a-photoimage-object-work-for-a-tkinter-label-imag
标签: python image tkinter background-image turtle-graphics