在kivy中保存价值

2024-04-28 06:45:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个小kivy程序,因为我想学习如何使用.json文件保存值。 我查阅了文件,发现了这个:http://kivy.org/docs/api-kivy.storage.html#module-kivy.storage 我试图用类似的方法,但是我得到了错误:ImportError: No module named storage.jsonstore

为什么不管用?在

这是我的代码:

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()

Tags: 文件fromimportjsonappbuilderbuttonstorage
1条回答
网友
1楼 · 发布于 2024-04-28 06:45:34

我以前从未使用过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()

编辑:1

它实际上工作得非常好。 试试这个改进的代码。在

你的文件

^{pr2}$

你的.kv文件

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

相关问题 更多 >