Made an LED User Defined Element, compated the code for updating to a single line

This commit is contained in:
PySimpleGUI 2020-06-27 06:49:10 -04:00
parent 0dd5e7900d
commit 3092388b36
1 changed files with 14 additions and 7 deletions

View File

@ -15,9 +15,18 @@ sg.theme('Light Brown 4')
CIRCLE = ''
CIRCLE_OUTLINE = ''
layout = [ [sg.Text('Status 1 '), sg.Text(CIRCLE_OUTLINE, text_color='green', key='-LED0-')],
[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-')],
def LED(color, key):
"""
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')]]
window = sg.Window('Window Title', layout, font='Any 16')
@ -26,10 +35,8 @@ while True:
event, values = window.read(timeout=200)
if event == sg.WIN_CLOSED or event == 'Exit':
break
# Loop through all of the LEDs and update. 25% of the time turn it off.
for i in range(3):
if randint(1,100) < 25:
window[f'-LED{i}-'].update(CIRCLE)
else:
window[f'-LED{i}-'].update(CIRCLE_OUTLINE)
window[f'-LED{i}-'].update(CIRCLE if randint(1, 100) < 25 else CIRCLE_OUTLINE)
window.close()