【问题标题】:Python Turtle mainloop() usagePython Turtle mainloop() 用法
【发布时间】:2016-07-07 18:35:23
【问题描述】:

我有来自online tutorial 的以下代码,通过制作一个在单击鼠标时改变状态的停止灯来学习基于事件的编程。这是我的全部代码:

import turtle

turtle.setup(400,500)
wn = turtle.Screen()
wn.title("Tess becomes a traffic light!")
wn.bgcolor("lightgreen")
tess = turtle.Turtle()

def draw_housing():
    tess.pensize(3)
    tess.color("black","darkgrey")
    tess.begin_fill()
    tess.forward(80)
    tess.left(90)
    tess.forward(200)
    tess.circle(40, 180)
    tess.forward(200)
    tess.left(90)
    tess.end_fill()

draw_housing()

tess.penup()
tess.forward(40)
tess.left(90)
tess.forward(40)
tess.shape("circle")
tess.shapesize(3)
tess.fillcolor("green")

state_num = 0

def nextFSMstate():
    global state_num
    if state_num == 0:
            tess.forward(70)
            tess.fillcolor("orange")
            state_num = 1
    elif state_num == 1:
            tess.forward(70)
            tess.fillcolor("red")
            state_num = 2
    else:
            tess.back(140)
            tess.fillcolor("green")
            state_num = 0

wn.onkey(nextFSMstate, "space")
wn.listen()
turtle.mainloop()
    # example says wn.mainloop() but I get error. This works though

在教程中,他们使用:

wn.mainloop()

但我得到了错误:

File "stopLights.py", line 51, in <module>
    wn.mainloop()
AttributeError: '_Screen' object has no attribute 'mainloop'

并且必须使用

turtle.mainloop()

为什么不一样?我在 Ubuntu 中使用 Python 2.7;该示例在 PyScripter 中。提前致谢。

【问题讨论】:

    标签: python python-2.7 tkinter turtle-graphics event-driven


    【解决方案1】:

    这似乎是教程中的错误。

    在第 4 行,他们定义了wn = turtle.Screen(),这意味着后面对wn.mainloop() 的调用等价于调用turtle.Screen().mainloop()

    这没有任何意义;正如错误消息所述,没有turtle.Screen().mainloop() 方法。 ,但是基础turtle 对象的.mainloop() 方法,这就是调用它的原因。

    【讨论】:

    • 仅供参考,turtle 对象没有 .mainloop() 方法 -- turtle.mainloop() 是对turtle 库的函数 调用. Python 2 将 mainloop() 定义为顶级函数,它是 TK.mainloop 的同义词——Python 3 将 mainloop() 定义为 Screen 方法(通过 TurtleScreen,通过 TurtleScreenBase),它调用 TK.mainloop(),并且像许多其他 Screen 方法也被定义为(以编程方式)作为顶级函数,它在唯一的 Screen 实例上调用同名方法。 Python 2 .mainloop() 的用法应该在 Python 3 中工作,但反过来取决于它是如何被调用的。
    • @cdlane >>turtle 对象没有 .mainloop() 方法——turtle.mainloop() 是对turtle 库的函数调用。 你能详细说明吗在那?我认为所有方法都是与对象关联的函数。
    • Python 既有不与对象关联的函数,也有与对象关联的方法。如果我打电话给type(turtle.mainloop),我会得到“功能”,但如果我打电话给type(turtle.Screen().mainloop),我会得到“方法”。定义您自己的顶级函数并在其上调用type()——您会得到什么?不要被&lt;class 'function'&gt; 返回值的class 部分所迷惑——函数 有一个共享类,但它们仍然不是方法。另一个令人困惑的是turtle. 前缀,通常只是一个库名称,但在某些代码中,是从 Turtle() 获得的海龟实例
    【解决方案2】:

    我怀疑这是您使用的 Python 版本与编写教程所针对的版本不同的问题。在我系统上的 Python 3.5 中,turtle.Screen 类的实例确实有一个 mainloop 方法,因此您报告为不工作的代码就可以了。

    如果您继续使用错误版本的 Python,您的代码可能存在其他问题(尽管您在问题中包含的内容似乎是 Python 2 和 Python 3 的公共子集的一部分,而不是 @987654323 @ 问题)。我强烈建议任何新的 Python 程序员从 Python 3 开始,只有在他们特别需要使用尚未移植的库时才返回 Python 2(并了解版本之间的差异)。 Python 3 是未来,现在库支持非常好!

    【讨论】:

    • 有道理。我从 3 开始,但不幸的是,我不得不回到 2.7,因为我必须使用旧代码
    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2020-08-29
    • 2023-03-26
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多