Python Turtle mainloop() usage

人盡茶涼 提交于 2020-05-31 07:11:39

问题


I have the following code from an online tutorial to learn event-based programming by making a stop light that changes state when the mouse is clicked. Here is the entirety of my code:

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

In the tutorial, they use:

wn.mainloop()

But I get the error:

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

and have to use

turtle.mainloop()

Why the difference? I am using Python 2.7 in Ubuntu; the example is in PyScripter. Thanks in advance.


回答1:


It appears to be an error in the tutorial.

On line 4, they define wn = turtle.Screen(), which means that the later call to wn.mainloop() is equivalent to calling turtle.Screen().mainloop().

This doesn't make any sense; as the error message states there is no .mainloop() method of turtle.Screen(). There is, however a .mainloop() method of the base turtle object, which is why calling that works.




回答2:


I suspect it's an issue with the version of Python you're using being different than the version the tutorial is written for. In Python 3.5 on my system, an instance of the turtle.Screen class does indeed have a mainloop method, so the code you report as not working would do just fine.

There may be other issues with your code if you continue to use the wrong version of Python (though what you've included in the question seems to be part of the common subset of Python 2 and Python 3 other than the mainloop issue). I'd strongly recommend any new Python programmer start with Python 3 and only go back to Python 2 (and learn the differences between the versions) if they specifically need to use a library that hasn't been ported yet. Python 3 is the future, and library support is pretty good these days!



来源:https://stackoverflow.com/questions/38252920/python-turtle-mainloop-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!