'How to make the text stay in the text box when app is reopened using tkinter

I'm just wondering how exactly I could make tkinter save the text in the current window so that when the app is closed and reopened, it would still be there. Any ideas?



Solution 1:[1]

# save the data - in this case '1234', converted to a string
# the value must be a string!
with open('save.txt', 'w') as f:
    f.write(str(1234))

# load the data from the file
with open('save.txt', 'r') as f:
    load = f.read()
    print(load)

This is a very barebones example and will only store one value in the file at a time since every call to f.write() will overwrite the existing file!

If you're looking for something a little more robust but still easy to implement, try the configparser module - it lets you read/write *.cfg/*.ini files, which are great if all you need are key-value relationships!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JRiggles