i have been trying to save the state of my application using python pickle module without success. my application works fine on mobile but whenever i close it, it loses all data. i want this application to be like a table notepad so that i can resume from where i left after closing the app. here is a part of my code to explain the problem. example.py
import pickle
class MyGrid(BoxLayout):
pass
if os.path.isfile("notepad"):
load = pickle.load(open("notepad", "rb"))
print(load)
textinput = StringProperty(load)
else:
textinput = StringProperty()
class Goat(App):
def build(self):
return MyGrid()
def save(self):
rectangle1 = [self.root.ids.a2.text, self.root.ids.a2.text]
pickle.dump(rectangle1, open("notepad", "wb"))
if __name__ == "__main__":
Goat().run()
here is my .kv file
<MyGrid>:
orientation: 'vertical'
ScrollView:
bar_width: 10
GridLayout:
id:gridlayout
cols :3
row_default_height: 90
height: self.minimum_height
size: 600, 500
padding: 5
pos: 0, -200
size_hint_y: None
##############################################box1
Label:
text : "A"
background_color: (1, 5, 0, 1)
font_size: 30
TextInput:
id : a1
multiline : False
font_size: 30
TextInput:
id : a2
multiline : False
font_size: 30
Button:
text: 'SUBMIT'
id : submit1
background_color: (1, 0, 0, 1)
on_release: app.save()
font_size: 39
when i type on the input field and press submit, a notepad.dat file is formed on the directory and the contents typed on the input field are saved.( i know this by print(load) on my .py file) my problem is bringing the contents of the dart file on the gui and making them stay there. i dont know how to do that. please help!
CodePudding user response:
You need to load the pickled data after the App
starts so that you can assign the pickled data to the widgets. Like this:
class Goat(App):
def on_start(self):
if os.path.isfile("notepad"):
load = pickle.load(open("notepad", "rb"))
self.root.ids.a1.text = load[0]
self.root.ids.a2.text = load[1]