简体   繁体   English

保存值

[英]Saving values in kivy

I wrote an small kivy program, because I wanted to learn how to save values using .json files. 我编写了一个小型的kivy程序,因为我想学习如何使用.json文件保存值。 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 我检查了文档,发现了这一点: http ://kivy.org/docs/api-kivy.storage.html#module-kivy.storage我尝试了类似的操作,但是出现了错误: 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()

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. 我以前从未使用过JsonStore,我将对其进行研究,然后将更新其答案,但是现在,您可以使用这种方式。

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 编辑:1

It actually works perfectly fine. 它实际上工作得很好。 Try this improved code. 试试这个改进的代码。

Your .py file 您的.py文件

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 您的.kv文件

<Lay_out>:
    orientation: 'vertical'
    TextInput:
        id:my_textinput
    Button:
        text: 'save'
        on_release: root.save(my_textinput.text)
    Button:
        text: 'acsess'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM