From b90c6baa5df19aee9124375395e673e6c34dd892 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 10 Oct 2020 11:17:00 -0400 Subject: [PATCH 1/2] Added examples of combining layout helper function with other elements on a single row --- DemoPrograms/Demo_Layout_Vertical.py | 30 ++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/DemoPrograms/Demo_Layout_Vertical.py b/DemoPrograms/Demo_Layout_Vertical.py index b34daf34..bb9e6dd1 100644 --- a/DemoPrograms/Demo_Layout_Vertical.py +++ b/DemoPrograms/Demo_Layout_Vertical.py @@ -32,14 +32,14 @@ def main(): [sg.Text('On row 1'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')], [sg.Button('OK')] ] - sg.Window('Window Title', layout).read(close=True) + sg.Window('Example 1', layout).read(close=True) # -------------------- Example 2 - Top aligned Text element using Column -------------------- layout = [ [sg.T('This uses a Column Element to align 1 element')], [sg.Col([[sg.Text('On row 1')]], vertical_alignment='top', pad=(0,0)), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')], [sg.Button('OK')] ] - sg.Window('Window Title', layout).read(close=True) + sg.Window('Example 2', layout).read(close=True) # -------------------- Example 3 - Top aligned Text element using Column -------------------- @@ -47,7 +47,7 @@ def main(): [sg.vtop(sg.Text('On row 1')), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')], [sg.Button('OK')] ] - sg.Window('Window Title', layout).read(close=True) + sg.Window('Example 3', layout).read(close=True) # -------------------- Example 4 - Top align an entire row -------------------- # Note that the vtop function takes a row as input and returns a row. DO NOT place [ ] around vtop @@ -56,10 +56,28 @@ def main(): sg.vtop([sg.Text('On row 1'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')]), [sg.Button('OK')] ] - sg.Window('Window Title', layout).read(close=True) + sg.Window('Example 4', layout).read(close=True) - # -------------------- Example 5 - Use function to align all rows in layout -------------------- + # -------------------- Example 5 - Top align portion of a row -------------------- + # You can combine 2 lists to make a row [a,b] + [c,d] = [a,b,c,d] + # To combine vtop with a normally specified row, add them vtop(a,b) + [c,d] = [a, b, c, d] (sorta) + layout = [ [sg.T('This layout uses the "vtop" for first part of row')], + sg.vtop([sg.Text('On row 1'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')]) + [sg.Text('More elements'), sg.CB('Last')], + [sg.Button('OK')] ] + + sg.Window('Example 5', layout).read(close=True) + + + # -------------------- Example 5B - Top align portion of a row -------------------- + # Same operation as adding the 2 lists, but instead unpacks vtop list directly into a row layout + layout = [ [sg.T('This layout uses the "vtop" for first part of row')], + [*sg.vtop([sg.Text('On row 1'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')]), sg.Text('More elements'), sg.CB('Last')], + [sg.Button('OK')] ] + + sg.Window('Example 5B', layout).read(close=True) + + # -------------------- Example 6 - Use function to align all rows in layout -------------------- layout = [ [sg.T('This layout has all rows top aligned using function')], [sg.Text('On row 1'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')], [sg.Text('On row 2'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 2')], @@ -67,7 +85,7 @@ def main(): layout = top_align_layout(layout) # pass in a layout, get a loyout back - sg.Window('Window Title', layout).read(close=True) + sg.Window('Example 6', layout).read(close=True) From 52c8dbf034e25e6c55e2cdf39c03e0bfc468be96 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Sat, 10 Oct 2020 11:17:12 -0400 Subject: [PATCH 2/2] New User Settings Demo --- DemoPrograms/Demo_User_Settings.py | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 DemoPrograms/Demo_User_Settings.py diff --git a/DemoPrograms/Demo_User_Settings.py b/DemoPrograms/Demo_User_Settings.py new file mode 100644 index 00000000..ff8796ad --- /dev/null +++ b/DemoPrograms/Demo_User_Settings.py @@ -0,0 +1,79 @@ +import PySimpleGUI as sg + +""" + Demo - User Settings + + Use the "user_settings" API calls to make a "settings window" + + This demo is very basic. The user_settings functions are used directly without a lookup table + or some other mechanism to map between PySimpleGUI keys and user settings keys. + + Copyright 2020 PySimpleGUI.org +""" + +SETTINGS_PATH = '.' + + +def make_window(): + """ + Creates a new window. The default values for some elements are pulled directly from the + "User Settings" without the use of temp variables. + + Some get_entry calls don't have a default value, such as theme, because there was an initial call + that would have set the default value if the setting wasn't present. Could still put the default + value if you wanted but it would be 2 places to change if you wanted a different default value. + + Use of a lookup table to map between element keys and user settings could be aded. This demo + is intentionally done without one to show how to use the settings APIs in the most basic, + straightforward way. + + If your application allows changing the theme, then a make_window function is good to have + so that you can close and re-create a window easily. + + :return: (sg.Window) The window that was created + """ + + sg.user_settings_filename(path=SETTINGS_PATH) + + sg.theme(sg.user_settings_get_entry('theme', 'DarkBlue2')) # set the theme + + layout = [[sg.Text('Settings Window')], + [sg.Input(sg.user_settings_get_entry('input', ''), k='-IN-')], + [sg.Listbox(sg.theme_list(), default_values=[sg.user_settings_get_entry('theme')], size=(15, 10), k='-LISTBOX-')], + [sg.CB('Option 1', sg.user_settings_get_entry('option1', True), k='-CB1-')], + [sg.CB('Option 2', sg.user_settings_get_entry('option2', False), k='-CB2-')], + [sg.T('Settings file = ' + sg.user_settings_filename())], + [sg.Button('Save'), sg.Button('Exit without saving', k='Exit')]] + + return sg.Window('A Settings Window', layout) + + +def settings_window(): + """ + Create and interact with a "settings window". You can a similar pair of functions to your + code to add a "settings" feature. + """ + + window = make_window() + current_theme = sg.theme() + + while True: + event, values = window.read() + if event in (sg.WINDOW_CLOSED, 'Exit'): + break + if event == 'Save': + # Save some of the values as user settings + sg.user_settings_set_entry('input', values['-IN-']) + sg.user_settings_set_entry('theme', values['-LISTBOX-'][0]) + sg.user_settings_set_entry('option1', values['-CB1-']) + sg.user_settings_set_entry('option2', values['-CB2-']) + + # if the theme was changed, restart the window + if values['-LISTBOX-'][0] != current_theme: + current_theme = values['-LISTBOX-'][0] + window.close() + window = make_window() + + +if __name__ == '__main__': + settings_window()