Made an LED User Defined Element, compated the code for updating to a single line
This commit is contained in:
parent
0dd5e7900d
commit
3092388b36
|
@ -15,9 +15,18 @@ sg.theme('Light Brown 4')
|
||||||
CIRCLE = '⚫'
|
CIRCLE = '⚫'
|
||||||
CIRCLE_OUTLINE = '⚪'
|
CIRCLE_OUTLINE = '⚪'
|
||||||
|
|
||||||
layout = [ [sg.Text('Status 1 '), sg.Text(CIRCLE_OUTLINE, text_color='green', key='-LED0-')],
|
def LED(color, key):
|
||||||
[sg.Text('Status 2 '), sg.Text(CIRCLE_OUTLINE, text_color='red', key='-LED1-')],
|
"""
|
||||||
[sg.Text('Status 3 '), sg.Text(CIRCLE_OUTLINE, text_color='blue', key='-LED2-')],
|
A "user defined element". In this case our LED is based on a Text element. This gives up 1 location to change how they look, size, etc.
|
||||||
|
:param color: (str) The color of the LED
|
||||||
|
:param key: (Any) The key used to look up the element
|
||||||
|
:return: (sg.Text) Returns a Text element that displays the circle
|
||||||
|
"""
|
||||||
|
return sg.Text(CIRCLE_OUTLINE, text_color=color, key=key)
|
||||||
|
|
||||||
|
layout = [ [sg.Text('Status 1 '), LED('Green', '-LED0-') ],
|
||||||
|
[sg.Text('Status 2 '), LED('blue', '-LED1-')],
|
||||||
|
[sg.Text('Status 3 '), LED('red', '-LED2-')],
|
||||||
[sg.Button('Exit')]]
|
[sg.Button('Exit')]]
|
||||||
|
|
||||||
window = sg.Window('Window Title', layout, font='Any 16')
|
window = sg.Window('Window Title', layout, font='Any 16')
|
||||||
|
@ -26,10 +35,8 @@ while True:
|
||||||
event, values = window.read(timeout=200)
|
event, values = window.read(timeout=200)
|
||||||
if event == sg.WIN_CLOSED or event == 'Exit':
|
if event == sg.WIN_CLOSED or event == 'Exit':
|
||||||
break
|
break
|
||||||
|
# Loop through all of the LEDs and update. 25% of the time turn it off.
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
if randint(1,100) < 25:
|
window[f'-LED{i}-'].update(CIRCLE if randint(1, 100) < 25 else CIRCLE_OUTLINE)
|
||||||
window[f'-LED{i}-'].update(CIRCLE)
|
|
||||||
else:
|
|
||||||
window[f'-LED{i}-'].update(CIRCLE_OUTLINE)
|
|
||||||
|
|
||||||
window.close()
|
window.close()
|
||||||
|
|
Loading…
Reference in New Issue