【发布时间】:2016-08-31 20:57:48
【问题描述】:
我正在尝试创建一个 python 绘画类型程序,您可以在其中使用海龟绘画。一切运行顺利,最终设计很棒。 我希望乌龟的形状是铅笔而不是箭头,所以我制作了一个简单的 .gif 文件并注册了形状,然后将乌龟变成了形状。 当我运行它时,它会这样说:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 "/Users/h205p6/PycharmProjects/Turtle Projects/python paint.py"
Traceback (most recent call last):
File "/Users/h205p6/PycharmProjects/Turtle Projects/python paint.py", line 124, in <module>
turtle.shape(pencil)
File "<string>", line 8, in shape
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 2681, in shape
self.turtle._setshape(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 2410, in _setshape
self._item = screen._createimage(screen._shapes["blank"]._data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.py", line 729, in _createimage
return self.cv.create_image(0, 0, image=image)
File "<string>", line 1, in create_image
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2314, in create_image
return self._create('image', args, kw)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2305, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage1" doesn't exist
Process finished with exit code 1
我根本不知道它为什么会这样。我已经尝试并试图让它工作,但我就是想不通
这是我的代码:
import turtle
import Tkinter
root = Tkinter.Tk()
tb = 6
ts = 1
def cs():
turtle.pendown()
turtle.circle(25)
turtle.penup()
def cb():
turtle.pendown()
turtle.circle(50)
turtle.penup()
def square():
turtle.pendown()
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.penup()
def triangle():
turtle.pendown()
turtle.forward(50)
turtle.left(120)
turtle.forward(50)
turtle.left(120)
turtle.forward(50)
turtle.left(120)
turtle.penup()
def green():
turtle.color("green")
def red():
turtle.color("red")
def blue():
turtle.color("blue")
def yellow():
turtle.color("yellow")
def reset():
turtle.color("black")
def white():
turtle.color("white")
def orange():
turtle.color("orange")
def big():
turtle.pensize(tb)
def small():
turtle.pensize(ts)
def clear():
turtle.clear()
def saveImg():
turtle.hideturtle()
name = raw_input('What would you like to name your drawing?: ')
tg = turtle.getscreen().getcanvas()
tg.postscript(file = name + ".eps")
print "You can find your .eps file in the finder/directory and open it to convert it to .pdf"
print "Or, follow this link to convert it to a .png file!"
print "http://image.online-convert.com/convert-to-png"
exit()
greenb = Tkinter.Button(root, text="green", fg ="green", command=green)
redb = Tkinter.Button(root, text="red", fg ="red", command=red)
blueb = Tkinter.Button(root, text="blue", fg ="blue", command=blue)
yellowb = Tkinter.Button(root, text="yellow", fg ="yellow", command=yellow)
black = Tkinter.Button(root, text="black", command=reset)
white = Tkinter.Button(root, text="white", command=white)
orangeb = Tkinter.Button(root, text="orange", fg ="orange", command=orange)
bigger = Tkinter.Button(root, text="big pensize", command=big)
smaller = Tkinter.Button(root, text="reset pensize", command=small)
clear = Tkinter.Button(root, text="clear", command=clear)
square = Tkinter.Button(root, text="create square", command=square)
triangle = Tkinter.Button(root, text="create triangle", command=triangle)
cb = Tkinter.Button(root, text="make big circle", command=cb)
cs = Tkinter.Button(root, text="make small circle", command=cs)
saveImgb = Tkinter.Button(root, text="save drawing", command=saveImg)
def gothere(event):
turtle.penup()
turtle.goto(event.x-360,340-event.y)
turtle.pendown()
def movearound(event):
turtle.goto(event.x-360,340-event.y)
def release(event):
turtle.penup()
def reset(event):
turtle.clear()
turtle.reset()
turtle.speed(0)
c=turtle.getcanvas()
c.bind("<Button-1>", gothere)
c.bind("<B1-Motion>", movearound)
c.bind("<ButtonRelease-1>", release)
c.bind("<Escape>",reset)
redb.pack()
orangeb.pack()
yellowb.pack()
greenb.pack()
blueb.pack()
white.pack()
black.pack()
bigger.pack()
smaller.pack()
cb.pack()
cs.pack()
triangle.pack()
square.pack()
clear.pack()
saveImgb.pack()
pencil = "/Users/h205p6/Desktop/ok.gif"
turtle.register_shape(pencil)
turtle.shape(pencil)
turtle.resizemode("auto")
s=turtle.Screen()
turtle.title("Python Paint")
root.title("Paint Tablet")
s.listen()
s.bgcolor("white")
turtle.mainloop()
root.mainloop()
请帮帮我!我真的很想让这个工作! 不要刻薄! 记得尊重你的长辈年轻人。 HackingYourNan 出来。
【问题讨论】:
-
我认为您不需要所有代码来说明这个问题。您可能想阅读stackoverflow.com/help/mcve。
标签: python gif shape turtle-graphics