import PySimpleGUI as sg

"""
    Demo Button Images & Theme stored in User Settings

    Short demo of some nice buttons obtained from the web.
    These are a good example of higher-end buttons that can be found on the web.
    Often the better buttons are in Photoshop format.  These all orignally had text as part of the button.
    Processing the buttons - The text was removed, the Photoshop image saved as PNG, PNG file converted to Base64

    Copyright 2020 PySimpleGUI.org
"""


def make_window(theme):
    """
    Sets theme to theme parameter and returns a window
    Creates the layout and the window object

    :param theme:   (str) the theme used to color the window
    :return:        (sg.Window) the Window you'll use in your event loop
    """
    sg.theme(theme)
    layout = [
        [sg.Text('More Buttons from the Web....', font='Default 20')],
        [
            sg.Button('My Text', image_data=image_sunken, font='Helvetica 12 bold italic', button_color=('white', sg.theme_background_color()),
                      border_width=0, ),
            sg.Button('More Text', image_data=image_large_rectangle, font='Helvetica 12 bold italic', button_color=('white', sg.theme_background_color()),
                      border_width=0, ),
            sg.Button('1/2 size', image_data=image_large_rectangle, image_subsample=2, button_color=('white', sg.theme_background_color()), border_width=0, ),
        ],
        [
            sg.Button(image_data=image_sunken, button_color=('white', sg.theme_background_color()), border_width=0, ),
            sg.Button(image_data=image_large_rectangle, button_color=('white', sg.theme_background_color()), border_width=0, ),
        ],
        [
            sg.Button(image_data=image_grey1, button_color=('black', sg.theme_background_color()), border_width=0),
            sg.Button(image_data=image_grey2, button_color=('black', sg.theme_background_color()), border_width=0),
        ],
        [
            sg.Button('I Like', image_data=image_grey1, font='Helvetica 12 bold italic', button_color=('black', sg.theme_background_color()), border_width=0, ),
            sg.Button('Nice too!', image_data=image_grey2, font='Helvetica 12', button_color=('black', sg.theme_background_color()), border_width=0, ),
        ],
        [sg.Combo(sg.theme_list(), default_value=theme, enable_events=True, key='-THEMES-')],
    ]

    return sg.Window('Nice Buttons Buddy', layout, finalize=True, use_default_focus=False)


def main():
    """
    main propgram.  It's a good practice to encapsulate your main program inside of a function
    """

    # Requires version 4.29.0.16+
    try:
        saved_theme = sg.user_settings_get_entry('theme', 'Python')
    except:
        print("Your PySimpleGUI version doesn't have user settings API\n",
              "You need PySimpleGUI version 4.29.0.16 and above")
        saved_theme = 'Python'

    window = make_window(saved_theme)

    while True:  # Event Loop
        event, values = window.read()
        if event == sg.WINDOW_CLOSED:
            break
        if values['-THEMES-']:
            window.close()
            window = make_window(values['-THEMES-'])
            try:
                sg.user_settings_set_entry('theme', values['-THEMES-'])
            except:
                pass  # already warned about settings at start
    window.close()


if __name__ == '__main__':
    image_sunken = b'[encoded data, 33292 characters]'
    image_large_rectangle = b'[encoded data, 34207 characters]'
    image_grey1 = b'[encoded data, 24560 characters]'
    image_grey2 = b'[encoded data, 24484 characters]'
    main()