使用 Zelle 图形模块单击鼠标时尝试循环移动红绿灯
Trying to move the traffic light in a loop when clicking the mouse with Zelle graphics module
from graphics import *
def trafficlight():
win = GraphWin()
box = Rectangle(Point(75, 25), Point(125, 175))
box.draw(win)
yellow = Circle(Point(100,100), 25)
yellow.setFill('yellow')
red = Circle(Point(100,50), 25)
red.setFill('red')
green = Circle(Point(100,150), 25)
green.setFill('green')
yellow.draw(win)
red.draw(win)
green.draw(win)
win.getMouse()
red.setFill('grey')
yellow.setFill('grey')
green.setFill('green')
win.getMouse()
red.setFill('grey')
yellow.setFill('yellow')
green.setFill('grey')
win.getMouse()
red.setFill('red')
yellow.setFill('grey')
green.setFill('grey')
win.getMouse()
trafficlight()
我的代码可以运行,但唯一的问题是我无法让函数循环,它在跳转到红色后停止,但它需要跳转到绿色,然后是黄色,然后循环跳转到红色。我试过使用函数 win.mianloop()
但这也不起作用。我想使用 while 循环,但我不知道该怎么做,有什么建议吗?
只需在您的函数中加入一个循环即可:
from graphics import *
def trafficlight():
win = GraphWin()
box = Rectangle(Point(75, 25), Point(125, 175))
box.draw(win)
yellow = Circle(Point(100,100), 25)
yellow.setFill('yellow')
red = Circle(Point(100,50), 25)
red.setFill('red')
green = Circle(Point(100,150), 25)
green.setFill('green')
yellow.draw(win)
red.draw(win)
green.draw(win)
win.getMouse()
while True: # Loop forever.
red.setFill('grey')
yellow.setFill('grey')
green.setFill('green')
win.getMouse()
red.setFill('grey')
yellow.setFill('yellow')
green.setFill('grey')
win.getMouse()
red.setFill('red')
yellow.setFill('grey')
green.setFill('grey')
win.getMouse()
trafficlight()
from graphics import *
def trafficlight():
win = GraphWin()
box = Rectangle(Point(75, 25), Point(125, 175))
box.draw(win)
yellow = Circle(Point(100,100), 25)
yellow.setFill('yellow')
red = Circle(Point(100,50), 25)
red.setFill('red')
green = Circle(Point(100,150), 25)
green.setFill('green')
yellow.draw(win)
red.draw(win)
green.draw(win)
win.getMouse()
red.setFill('grey')
yellow.setFill('grey')
green.setFill('green')
win.getMouse()
red.setFill('grey')
yellow.setFill('yellow')
green.setFill('grey')
win.getMouse()
red.setFill('red')
yellow.setFill('grey')
green.setFill('grey')
win.getMouse()
trafficlight()
我的代码可以运行,但唯一的问题是我无法让函数循环,它在跳转到红色后停止,但它需要跳转到绿色,然后是黄色,然后循环跳转到红色。我试过使用函数 win.mianloop()
但这也不起作用。我想使用 while 循环,但我不知道该怎么做,有什么建议吗?
只需在您的函数中加入一个循环即可:
from graphics import *
def trafficlight():
win = GraphWin()
box = Rectangle(Point(75, 25), Point(125, 175))
box.draw(win)
yellow = Circle(Point(100,100), 25)
yellow.setFill('yellow')
red = Circle(Point(100,50), 25)
red.setFill('red')
green = Circle(Point(100,150), 25)
green.setFill('green')
yellow.draw(win)
red.draw(win)
green.draw(win)
win.getMouse()
while True: # Loop forever.
red.setFill('grey')
yellow.setFill('grey')
green.setFill('green')
win.getMouse()
red.setFill('grey')
yellow.setFill('yellow')
green.setFill('grey')
win.getMouse()
red.setFill('red')
yellow.setFill('grey')
green.setFill('grey')
win.getMouse()
trafficlight()