import PySimpleGUI as sg

"""
    Toggle Button Demo
    The background color of the button toggles between on and off
    Two versions are present... a simple button that changes text and a graphical one
    A HUGE thank you to the PySimpleGUI community memeber that donated his time and skill in creating the buttons!
    The text of the button toggles between Off and On
    
    Copyright 2021 PySimpleGUI
"""

def main():
    layout = [[sg.Text('A toggle button example')],
              [sg.Text('A graphical version'),
               sg.Button('', image_data=toggle_btn_off, key='-TOGGLE-GRAPHIC-', button_color=(sg.theme_background_color(), sg.theme_background_color()), border_width=0)],
             [sg.Button('On', size=(3, 1), button_color='white on green', key='-B-'),  sg.Button('Exit')]]

    window = sg.Window('Toggle Button Example', layout)

    down = graphic_off = True
    while True:             # Event Loop
        event, values = window.read()
        print(event, values)
        if event in (sg.WIN_CLOSED, 'Exit'):
            break
        elif event == '-B-':                # if the normal button that changes color and text
            down = not down
            window['-B-'].update(text='On' if down else 'Off', button_color='white on green' if down else 'white on red')
        elif event == '-TOGGLE-GRAPHIC-':   # if the graphical button that changes images
            graphic_off = not graphic_off
            window['-TOGGLE-GRAPHIC-'].update(image_data=toggle_btn_off if graphic_off else toggle_btn_on)

    window.close()

if __name__ == '__main__':
    toggle_btn_off = b'[encoded data, 5416 characters]'
    toggle_btn_on = b'[encoded data, 6048 characters]'
    main()