【发布时间】: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