Changed demo to directly call user_settings within the layout

This commit is contained in:
PySimpleGUI 2020-10-12 17:24:14 -04:00
parent 13ad1c27b1
commit ea03abb6d0
1 changed files with 14 additions and 11 deletions

View File

@ -79,24 +79,27 @@ def settings_window():
def save_previous_filename_demo(): def save_previous_filename_demo():
""" """
Saving the previously selected filename.... Saving the previously selected filename....
A 2-line demo of one of the likely most popular use of user settings A demo of one of the likely most popular use of user settings
* Get the previously entered filename * Use previous input as default for Input
* Use previous as default for Input
* When a new filename is chosen, write the filename to user settings * When a new filename is chosen, write the filename to user settings
""" """
prior_filename = sg.user_settings_get_entry('filename', '') # Notice that the Input element has a default value given (first parameter) that is read from the user settings
layout = [[sg.Text('Enter a filename:')], layout = [[sg.Text('Enter a filename:')],
[sg.Input(prior_filename, key='-IN-'), sg.FileBrowse()], [sg.Input(sg.user_settings_get_entry('filename', ''), key='-IN-'), sg.FileBrowse()],
[sg.B('OK')]] [sg.B('Save'), sg.B('Exit Without Saving', key='Exit')]]
event, values = sg.Window('Filename Example', layout).read(close=True) window = sg.Window('Filename Example', layout)
# If user clicked OK, then save the filename for next time while True:
if event == 'OK': event, values = window.read()
if event in(sg.WINDOW_CLOSED, 'Exit'):
break
elif event == 'Save':
sg.user_settings_set_entry('filename', values['-IN-']) sg.user_settings_set_entry('filename', values['-IN-'])
window.close()
if __name__ == '__main__': if __name__ == '__main__':
sg.user_settings_filename(path=SETTINGS_PATH) # Set the location for the settings file sg.user_settings_filename(path=SETTINGS_PATH) # Set the location for the settings file