Python 龟形 "pyimage1"

Python Turtle Shape "pyimage1"

我正在尝试创建一个 python 绘画类型程序,您可以在其中使用 turtle 进行绘画。一切 运行 都很顺利,最终的设计也很棒。 我希望乌龟的形状是铅笔而不是箭头,所以我制作了一个简单的 .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 出来。

image "pyimage1" doesn't exist

我对这个错误的理解是,它是由于您在 Tk 已经 运行ning(或即将)时在 turtle 图形环境中调用 Tkinter.Tk() 引起的——即您已经创建两个 Tk 实例发生冲突。

要测试这个想法,您可以尝试以下更改:

# root = Tkinter.Tk()
turtle.Screen()
root = Tkinter.Toplevel()

如果一切顺利,你应该可以运行你的程序与你的铅笔形乌龟。