Web Analytics Made Easy - Statcounter
Skip to content

Save and Load Program Settings

There is an entire set of API calls now available to you in PySimpleGUI to help with "settings".

Please check out the demo programs as there are numerous demos that have calls to the settings APIs.

There is also a section in the main documentation about these APIs. They are detailed in the call reference as well.

The basics are that in your layout or before your layout, you'll read your settings. If your program changes any settings, they will immediately be saved to your settings file. You never have to worry about loading or saving the settings file. It happens automatically. Just start calling the get and set calls if using the "function interface".

If you do not set a filename, then the name of your .py or .pyw file will be used. Beware, however, that turning your program into an EXE can impact the settings file as your filename won't look the same to the Python code. If you're going to turn your code into an EXE, then it's best to explicitly set the filename.

This statement sets the filename. Note that the path is not indicated in this example. PySimpleGUI will store all the settings in the default settings folder if you don't specify one.

sg.user_settings_filename(filename='DaysUntil.json')

Reading a setting can be as easy as this call:

theme = sg.user_settings_get_entry('-theme-', 'Dark Gray 13')

The first parm is the "key" and the second is the default value if no setting is found.

Saving a setting can be done with this call:

sg.user_settings_set_entry('-theme-', my_new_theme)

Take a look at the main documentation for the Object interface if you would prefer it over the function based interface. These API calls are used in any demo program that has settings and for all PySimpleGUI projects that have settings. They're super SIMPLE to use and work well.

A Simple Save Filename Example

import PySimpleGUI as sg

def save_previous_filename_demo():
    """
    Saving the previously selected filename....
    A demo of one of the likely most popular use of user settings
    * Use previous input as default for Input
    * When a new filename is chosen, write the filename to user settings
    """

    # 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:')],
              [sg.Input(sg.user_settings_get_entry('-filename-', ''), key='-IN-'), sg.FileBrowse()],
              [sg.B('Save'), sg.B('Exit Without Saving', key='Exit'), sg.T('(or click X to close without saving)')]]

    window = sg.Window('Filename Example', layout)

    while True:
        event, values = window.read()
        if event in (sg.WINDOW_CLOSED, 'Exit'):
            break
        elif event == 'Save':
            sg.user_settings_set_entry('-filename-', values['-IN-'])

    window.close()


if __name__ == '__main__':
    sg.user_settings_filename(path='.')  # Set the location for the settings file
    # Run a couple of demo windows
    save_previous_filename_demo()