【发布时间】:2021-12-06 01:24:06
【问题描述】:
我刚开始学习 Python。我正在尝试使用海龟模块。在同一个目录下,我有两个文件,script.py 和 test.py。
在script.py中,我有一个函数写如下:
import turtle
def circle(x, y, r, colour)
turtle.showturtle()
turtle.shape('classic')
turtle.penup()
turtle.setx(x)
turtle.sety(y)
turtle.pendown()
turtle.pencolor(colour)
turtle.fillcolor(colour)
turtle.begin_fill()
turtle.circle(r)
turtle.end_fill()
turtle.hideturtle()
circle(100, 100, 100, 'blue')
turtle.mainloop()
运行 script.py 会产生蓝色圆圈。
然后我想将 script.py 导入 test.py。我做了以下事情:
import script as file2
file2.circle(100, 100, 100, 'red')
*编辑:我之前错误地将其写为 file2.script(),但它在 python 中被正确记录。
当我运行 test.py 而不为 script.py 创建配置,没有在 script.py 中调用 circle 时,我可以绘制圆并在 test.py 中编辑它的变量。
在我编辑并保存配置后(只需使用 script.py 的目录并让它自动填充),该函数无法运行,并且出现以下错误。
Traceback (most recent call last):
File "C:\Users\username\PycharmProjects\project2\test.py", line 3, in <module>
circle.draw_circle(100, 100, 100, 'red')
File "C:\Users\username\PycharmProjects\project2\script.py", line 5, in circle
turtle.showturtle()
File "<string>", line 5, in showturtle
turtle.Terminator
Process finished with exit code 1
我似乎无法恢复发生的事情。我做错了什么,如何将循环函数从 script.py 导入 test.py?提前谢谢你。
【问题讨论】:
标签: python python-turtle