python未检测到图像

时间:2020-10-29 03:51:37

标签: python python-3.x turtle-graphics python-turtle

我正在做这个简单的python turtle程序,由于某种原因,该程序未检测到图像文件。我在VSCode和Repl中都运行了程序,但都出现了错误。我已经仔细检查了图像名称及其格式

#-----import turtle-----
import turtle as trtl

#-----setup-----
pear_image = "pear.png"
apple_image = "apple.png"

main = trtl.Screen()
main.setup(width=0.5, height=0.5)
main.bgpic("background.jpg")
main.addshape(pear_image)
main.addshape(apple_image)


pear = trtl.Turtle()
pear.shape(pear_image)
pear.penup()

apple = trtl.Turtle()
apple.shape(apple_image)
apple.penup()

#-----functions-----
# given a turtle, set that turtle to be shaped by the image file
def draw_pear(active_pear):
  active_pear.shape(pear_image)
  main.update()

def pear_down():
  pear.left(-120)
  pear.forward(100)


def draw_apple(active_apple):
  active_apple.shape(apple_image)
  main.update()

def apple_down():
  apple.left(-60)
  apple.forward(100)

def write_pear():
  main.goto(0,-80)
  main.pendown()
  main.color("yellow")
  main.write("Pear", font=("Arial", 74, "bold"))

def write_apple():
  main.goto(0,-80)
  main.pendown()
  main.color("red")
  main.write("Apple", font=("Arial", 74, "bold"))

#-----function calls-----

pear_down()

apple_down()

main.onkeypress(write_pear, "p")

main.onkeypress(write_apple, "a")

main.mainloop()

谢谢您的帮助!

1 个答案:

答案 0 :(得分:-1)

您需要为我认为的图像设置路径。像“ c:/apple.png”一样,请确保正确的切线方式。