0

我编写了一个简单的 python 仪表板,使用 guizero 和 gpiozero 来打开和关闭 LED。我只有 LED 有问题,它们只亮了 1 秒钟就熄灭了。我尝试使用 time.sleep() 来查看 LED 是否会保持亮起,但不幸的是事实并非如此,这就是我问这个问题的原因。(我是 Python 新手 + 为了我的隐私,我更改了一些行)

这是我的代码:

#!/usr/bin/env python
"""
When the "ok" buttons are pressed, the lights will turn on.
"""


from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
from guizero import App, PushButton, Box, Text


__author__ = "Senne De Winter"
__email__ = "senne.dewinter@...."
__status__ = "Development"


IP = PiGPIOFactory(host='192.....')
LED1 = LED(21, pin_factory=IP)
LED2 = LED(20, pin_factory=IP)
LED3 = LED(16, pin_factory=IP)


def turn_led1_on():
    if LAMP1.bg == "white":
        LAMP1.tk.configure(bg="red")
        LED1.on()

    elif LAMP1.bg == "red":
        LAMP1.tk.configure(bg="white")
        LED1.off()


def turn_led2_on():
    if LAMP2.bg == "white":
        LAMP2.tk.configure(bg="red")
        LED2.on()

    elif LAMP2.bg == "red":
        LAMP2.tk.configure(bg="white")
        LED2.off()


def turn_led3_on():
    if LAMP3.bg == "white":
        LAMP3.tk.configure(bg="red")
        LED3.on()

    elif LAMP3.bg == "red":
        LAMP3.tk.configure(bg="white")
        LED3.off()


app = App(title="Dashboard", layout="grid", width=400)

invis_1 = Box(app, grid=[0, 0], width=80)
invis_2 = Box(app, grid=[4, 0], width=20)
invis_3 = Box(app, grid=[8, 0], width=20)

invis_4 = Box(app, grid=[0, 1], width=80, height=10)
invis_5 = Box(app, grid=[4, 1], width=20, height=10)
invis_6 = Box(app, grid=[8, 1], width=20, height=10)

invis_7 = Box(app, grid=[0, 2], width=80)
invis_8 = Box(app, grid=[4, 2], width=20)
invis_9 = Box(app, grid=[8, 2], width=20)

lamp1 = Box(app, border=True, grid=[2,0])
lamp2 = Box(app, border=True, grid=[6,0])
lamp3 = Box(app, border=True, grid=[12,0])

LAMP1 = Text(lamp1, text="LAMP 1")
LAMP1.tk.configure(background="white")

LAMP2 = Text(lamp2, text="LAMP 2", bg="white")
LAMP2.tk.configure(background="white")

LAMP3 = Text(lamp3, text="LAMP 3", bg="white")
LAMP3.tk.configure(background="white")

button1 = PushButton(app, turn_led1_on, text="OK 1", grid=[2,2])
button2 = PushButton(app, turn_led2_on, text="OK 2", grid=[6,2])
button3 = PushButton(app, turn_led3_on, text="OK 3", grid=[12,2])

app.display()

不可见框是仪表板布局的不可见框。

4

1 回答 1

0

根据我所看到的,您需要使用它来切换您的LED1.on()和您LED1.off()LED1.toggle(),您可以按下仪表板上的按钮来打开和关闭它们。

这是一个例子

def exit_app():
sys.exit()

def toggle_led():
    led_01.toggle()
    led_02.toggle()
    led_03.toggle()
    led_04.toggle()
    if led_01.is_lit:
        ledButton.text = "LED OFF"
    else:
        ledButton.text = "LED ON"

def led_blink():
    for i in range(10):
        led_01.on()
        led_02.on()
        led_03.on()
        led_04.on()
        time.sleep(1)
        led_01.off()
        led_02.off()
        led_03.off()
        led_04.off()

# App
app = App(title="Project house", height=400, width=400)

# Boxes
displayBox = Box(app, border= True, height=35, width=400)
buttonBox = Box(app, layout="grid", align="top")

# Title
title = Text(displayBox, text="Push a button")
title.text_size = 20

# Buttons
ledButton = PushButton(buttonBox, toggle_led, text="LED ON", grid=[0,0])
ledButton.text_size = 25
blinkButton = PushButton(buttonBox, led_blink, text="Push to party", grid=[9,0])
blinkButton.text_size= 25
exitButton = PushButton(app, exit_app, text="EXIT", align="bottom", width=15 , 
height=2)
exitButton.text_size = 32

app.display()
于 2020-12-01T09:39:07.010 回答