Home > database >  UserSettings list in PySimpleGUI is being changed without something being explicitly assigned to it
UserSettings list in PySimpleGUI is being changed without something being explicitly assigned to it

Time:08-02

I have a list, I add things to it and for some reason the PySimpleGUI usersettings sees all the values I added to the list and just grabs them. The only place my list and usersettings list are connected is in the beginning of the script, outside the event loop.

You see in the debug window, as you add items to mylist, settings['-list-'] changes too. How? If you comment mylist = settings['-list-'] and uncomment mylist with colors, it stops doing this obviously, there's no more connection between them at all.

There's no settings["-list-"] = mylist assingment in the event loop that would do it. The bool switch behaves as normal, usersettings doesn't see it and doesn't assign anything implicitly.

On pysimplegui.org Mike says: "Also, because the settings automatically save after every update, it can be easy to accidently overwrite a previously saved setting. If you want to avoid this, then perhaps it's best that you work with a dictionary within your code and then explicitly save your dictionary when you're ready to commit it to disk."

Is it related to my problem? I don't update settings. The settings update themselves.

python 3.8.10 PySimpleGUI 4.60.1

import PySimpleGUI as sg

settings = sg.UserSettings(filename='b.json', path='.')

debug = sg.Print
debug('debug', keep_on_top=False)

if settings['-list-'] == None:
    settings['-list-'] = []

if settings['-switch-'] == None:    
    settings['-switch-'] = True

TF_switch = settings['-switch-']
#TF_switch = False

# That's why I'm here, to remember the user settings @Obi-Wan Kenobi, probably
mylist = settings['-list-']
#??? it's not even settings['-list-'] = mylist, but it behaves like that,
#and we don't come here again ever, the rest of the time we're in the event loop, right?

#mylist = ['Red', 'Green', 'Blue', 'Yellow', 'Orange', 'Purple']

debug('initial list:', mylist)
debug('initial switch:', TF_switch)

layout = [  [sg.Input(k='-input-')],
            [sg.Button('Add', k='add')],
            [sg.Button('Switch', k='-switch-')]]

window = sg.Window('', layout)

while True:
    
    event, values = window.read()

    if event == 'add':

        item_to_add = values['-input-']
        
        if item_to_add not in mylist:

            mylist.append(item_to_add)

            debug('appended item to mylist:', item_to_add)
            #you see? there's no settings["-list-"] = mylist here,
            #why is settings['-list-'] changing?

            item_to_add = ''

        #if item_to_add not in settings["-list-"]:
            #settings["-list-"] = mylist
            #debug('added item to settings["-list-"]')
        
        #if you uncomment this line,
        # it even saves to the json
        #settings.save(filename='b.json', path='.')
    
    if event == '-switch-':
        if TF_switch:
            TF_switch=False
        else: TF_switch=True

        #switch doesn't change the settings, as it should, there's no settings["-switch-"] = TF_switch
        # although it was assigned a value of settings['-switch-'] before the event loop

    debug('TF_switch:', TF_switch)
    debug('mylist:', mylist)
    debug('settings:', settings)

    if event == sg.WIN_CLOSED:
        break

window.close()

CodePudding user response:

When you do this: mylist = settings['-list-'] both mylist and settings['-list-'] are pointing to the same list. If you want them to stay separated, you might be interested in copying the list, using the copy() method.

mylist = settings['-list-'].copy()

Note that copy() is a shallow copy. If there is nested data in that list, a deepcopy() might be more appropriate. See enter image description here

  • Related