I wrote a simple python dashboard, using guizero and gpiozero, to turn LEDS on and off. I only have a problem with the LEDS, they only turn on for 1 second before they turn off. I tried to use time.sleep() to see if the LEDS would stay on, but unfortunately that was not the case and that's why I'm asking this question. (I'm new to Python + I changed some lines for my privacy)
This is my code:
#!/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()
The invis boxes are invisible boxes for the layout of the dashboard.
Based on what I can see you need to switch your
LED1.on()
and yourLED1.off()
withLED1.toggle()
with this you can push a button on your dashboard to turn them on and off.Here is a example