How do I make Python remember settings? How do I make Python remember settings? tkinter tkinter

How do I make Python remember settings?


Write the scale value to a file and read it in on startup. Here's one way to do it (roughly),

CONFIG_FILE = '/path/to/config/file'root.sclX = ...try:    with open(CONFIG_FILE, 'r') as f:        root.sclX.set(int(f.read()))except IOError:    # this is what happens if the file doesn't exist    pass...root.mainloop()# this needs to run when your program exitswith open(CONFIG_FILE, 'w') as f:    f.write(str(root.sclX.get()))

Obviously you could make it more robust/intricate/complicated if, for instance, you want to save and restore additional values.


Just before the mainloop:

import cPicklewith open('myconfig.pk', 'wb') as f:  cPickle.dump(f, root.config(), -1)  cPickle.dump(f, root.sclX.config(), -1)

and, on subsequent runs (when the .pk file is already present), the corresponding cPickle.load calls to get it back and set it with ...config(**k) (also needs some trickery to confirm to cPickle that the pickled configuration is safe to reload, unfortunately).


You can tell the program to write a file called for example "save.txt" with the parameter and then load it in further executions:

Is there any "save.txt"?

No: Write a new save file with the parameter.Yes: Read the parameter and pass it to the variable.

If the parameter is updated then rewrite it in the file.

I'm not expert on Python but, there should be some nice library to read and write files :)