From 3092388b360c30d464a3e27515045b077c29721f Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 27 Jun 2020 06:49:10 -0400 Subject: [PATCH] Made an LED User Defined Element, compated the code for updating to a single line --- .../Demo_LED_Indicators_Text_Based.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/DemoPrograms/Demo_LED_Indicators_Text_Based.py b/DemoPrograms/Demo_LED_Indicators_Text_Based.py index cc60caef..43e60a71 100644 --- a/DemoPrograms/Demo_LED_Indicators_Text_Based.py +++ b/DemoPrograms/Demo_LED_Indicators_Text_Based.py @@ -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()