首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当按下吉兹罗按钮时,我的LEDS为什么不亮?

当按下吉兹罗按钮时,我的LEDS为什么不亮?
EN

Stack Overflow用户
提问于 2020-11-30 19:31:51
回答 1查看 86关注 0票数 0

我编写了一个简单的python仪表板,使用guizero和gpiozero来打开和关闭LEDS。我只对LEDS有一个问题,他们只亮了一秒钟就熄灭了。我尝试使用time.sleep()查看LEDS是否会持续亮起,但不幸的是情况并非如此,这就是我问这个问题的原因。(我是Python新手,出于隐私考虑,我修改了一些代码行)

这是我的代码:

代码语言:javascript
复制
#!/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()

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

EN

回答 1

Stack Overflow用户

发布于 2020-12-01 17:39:07

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

下面是一个例子

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65072765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档