我如何在python turtle中导入图像?

mgdq6dx1  于 2023-02-15  发布在  Python
关注(0)|答案(1)|浏览(254)

我做了一个python程序,我不知道为什么它不工作。
我试过这个代码:

import turtle
screen=turtle.Screen()
opened_door="C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\opened_door.gif"
closed_door="C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\closed_door.gif"
screen.addshape(opened_door)
screen.addshape(closed_door)
t=turtle.Turtle()
t.shape(closed_door)

但每次我运行程序它都给我这样的错误:

File "c:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\Monty Hall.py", line 3
    opened_door="C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\opened_door.gif"
                                                                                ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

错误在哪里?

yftpprvb

yftpprvb1#

文件路径包含反斜杠,这是Python中的转义字符,要绕过这个问题,可以在字符串前面加上r

opened_door=r"C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\opened_door.gif"
closed_door=r"C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\closed_door.gif"

相关问题