0
from guizero import App, Window, Drawing, PushButton, Text
from time import sleep

def redon():
    draw.oval (325, 550, 575, 800, color="#f70000")
def redoff():
    draw.oval (325, 550, 575, 800, color="#520404")
def greenon():
    draw.oval (325, 40, 575, 280, color="#08d10f")
def greenoff():
    draw.oval (325, 40, 575, 280, color="#072e04")
def greenblink():
    draw.oval (325, 40, 575, 280, color="#08d10f")
    sleep(1)
    draw.oval (325, 40, 575, 280, color="#072e04")
def yellowon():
    draw.oval (325, 540, 575, 290, color="yellow")
def yellowoff():
    draw.oval (325, 540, 575, 290, color="#59580b")
def blueon():
    draw.oval (55, 550, 305, 800, color = "#1594b0")
def blueoff():
    draw.oval (55, 550, 305, 800, color = "#0a3842")
def sequenceon():
    seq = True
    while seq == True:
        draw.oval (325, 40, 575, 280, color="#08d10f")  #green on
        for i in range(0, 5): #left turn blink
            draw.oval (55, 550, 305, 800, color = "#1594b0") #blue on
            sleep (0.5)
            draw.oval (55, 550, 305, 800, color = "#0a3842") #blue off
            sleep (0.5)
        draw.oval (325, 40, 575, 280, color="#072e04") #green off
        draw.oval (325, 540, 575, 290, color="yellow") #yellow on
        sleep (1)
        draw.oval (325, 550, 575, 800, color="#f70000") #red on
        sleep (1)
        draw.oval (325, 550, 575, 800, color="#520404") #red off
def sequenceoff():
    seq = False
    draw.oval (325, 550, 575, 800, color="#520404") #red off
    draw.oval (325, 40, 575, 280, color="#072e04") #green off
    draw.oval (55, 550, 305, 800, color = "#0a3842")#blue off
    draw.oval (325, 540, 575, 290, color="#59580b") #yellow off


main = App(title = "Traffic Light View", width=1000, height=1000)
draw = Drawing(main, width = 1000, height = 1000)
draw.rectangle (300,10,600,810, color = "black")
draw.oval (325, 40, 575, 280, color = "#072e04")
draw.oval (325, 540, 575, 290, color = "#59580b")
draw.oval (325, 550, 575, 800, color = "#520404")
draw.rectangle (45, 540, 315, 810, color="black")
draw.oval (55, 550, 305, 800, color = "#0a3842")
control = Window(main, title="Controller", layout = "grid", height="200", width="700")

greent = Text(control, "GREEN", grid=[0,1])
greenon1 = PushButton(control, text="Green Light - ON", command=greenon, grid=[0,2])
greenoff1 = PushButton(control, text="Green Light - OFF", command=greenoff, grid=[0,3])
greenblink1 = PushButton(control, text="Green Light -  Blink", command=greenblink, grid=[0,4])

yellowt = Text(control, "YELLOW", grid=[1,1])
yellowon1 = PushButton(control, text="Yellow Light - ON", command = yellowon, grid =[1,2])
yellowoff1 = PushButton(control, text="Yellow Light - OFF", command=yellowoff, grid=[1,3])

redt = Text(control, "RED", grid = [2,1])
redon1 = PushButton(control, text="Red Light - ON", command=redon, grid=[2,2])
redoff1 = PushButton(control, text="Red Light - OFF", command=redoff, grid=[2,3])

bluet = Text(control, "BLUE", grid= [3,1])
blueon1 = PushButton(control, text="Blue Light - ON", command=blueon, grid=[3,2])
blueoff1 = PushButton(control, text="Blue Light - OFF", command=blueoff, grid=[3,3])

sequencet =  Text(control, "SEQUENCE", grid=[4,1])
sequenceon1 = PushButton(control, text="Sequence - Start", command=sequenceon, grid=[4,2])
sequenceoff1 = PushButton(control, text = "Sequence - Stop", command=sequenceoff, grid=[4,3])
main.display()

当我运行代码并单击 greenblink() 或 sequenceon() 按钮时,按钮将按住,表示代码正在休眠,然后它将运行以下代码。为什么 sleep 运行在函数中的所有其他代码之前?

此代码是用 python 编写的,用于 3b rasberry pi。

4

0 回答 0