[英]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()
我的代碼運行,但唯一的問題是我無法讓 function 循環,它在跳到紅色后停止,但它需要跳到綠色,然后是黃色,然后是循環中的紅色。 我試過使用 function win.mianloop()
但這也不起作用。 我想使用一個while循環,但我不知道如何 go 這樣做,有什么建議嗎?
只需在您的 function 中放一個循環:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.