【问题标题】:Error when importing function that uses Turtle from a different file从其他文件导入使用 Turtle 的函数时出错
【发布时间】: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


    【解决方案1】:

    为了从script.py tp 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()
        turtle.mainloop()
    

    test.py:

    import script as file2
    
    file2.circle(100, 100, 100, 'red')
    

    通过这样做,您的代码仅在调用函数时运行。

    【讨论】:

    • 感谢您的回复!你提议的方式醒了!但是,当我尝试将其放入更大的函数中时,我又遇到了问题。我会把问题贴在下面,如果你有时间看,我真的很感激
    • 您可以通过接受或支持答案来感谢我 :)
    • 顺便说一句,你可以在这里发布你的问题,或者只是编辑问题
    • 这不能正常工作。添加第二个圈子电话,例如file2.circle(50, 50, 50, 'blue') 并且您不会看到第二个圆圈,因为第一个调用将控制权移交给 tkinter 的事件循环,并且在海龟环境被拉下之前不会返回。
    • 只需从 Script.py 文件中删除 turtle.mainloop()。这将在红色圆圈上绘制蓝色圆圈
    【解决方案2】:

    我会选择这样的安排:

    脚本.py

    import turtle
    
    def circle(x, y, radius, colour):
        turtle.showturtle()
        turtle.penup()
        turtle.setposition(x, y)
        turtle.pendown()
        turtle.color(colour)
        turtle.begin_fill()
        turtle.circle(radius)
        turtle.end_fill()
        turtle.hideturtle()
    

    test.py

    import turtle
    import script as file2
    
    turtle.hideturtle()
    turtle.shape('classic')
    
    file2.circle(100, 100, 100, 'red')
    file2.circle(50, 50, 50, 'blue')
    
    turtle.mainloop()
    

    这应该允许您多次调用 script.py 中的circle 函数。

    【讨论】:

    • 感谢您的回复!作为后续问题,在 test.py 中是否需要导入海龟?它不会与 script.py 一起导入吗?
    • @AppleJuice,你可以试试看。我会亲自离开导入,因为 test.py 引用了海龟方法并且(应该)不知道 script.py 是否/如何导入海龟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多