'Saving values in kivy
I wrote an small kivy program, because I wanted to learn how to save values using .json files.
I checked out the documetation and found this:http://kivy.org/docs/api-kivy.storage.html#module-kivy.storage
I tried to to it similar,but I got the error:ImportError: No module named storage.jsonstore
Why doesn't it work?
Here is my code:
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.storage.jsonstore import JsonStore
from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
class Layout(BoxLayout):
def save(self, vinput):
store = JsonStore('hello.json')
store.put('tito', inpud=vinput)
ROOT = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
TextInput:
id:my_textinput
Button:
text: 'save'
Button:
text: 'acsess'
''')
class Caption(App):
def build(self):
Window.clearcolor = (0, 0, 1, 1)
return ROOT
if __name__ == '__main__':
Caption().run()
Solution 1:[1]
I have never used JsonStore before, I will have a look at it, then will update the answer, but for now, you could do it this way.
import json
#If you want to write in your JSON file.
out_file = open("your_file.json","w")
json.dump(result,out_file, indent=4)
# result ^ contains your data.
# indent = 4 is to make your file more readable.
out_file.close()
#If you want to read your JSON file.
in_file = open("your_file.json","r")
result = json.load(in_file)
in_file.close()
EDIT: 1
It actually works perfectly fine. Try this improved code.
Your .py file
from kivy.app import App
from kivy.storage.jsonstore import JsonStore
from kivy.uix.boxlayout import BoxLayout
class Lay_out(BoxLayout):
def save(self, vinput):
store = JsonStore('hello.json')
store.put('tito', inpud=vinput)
class CaptionApp(App):
def build(self):
return Lay_out()
if __name__ == '__main__':
CaptionApp().run()
Your .kv file
<Lay_out>:
orientation: 'vertical'
TextInput:
id:my_textinput
Button:
text: 'save'
on_release: root.save(my_textinput.text)
Button:
text: 'acsess'
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 |
