使用JSON文件作为Kivy应用程序中的存储时,.json文件的权限被拒绝

时间:2014-10-12 06:38:35

标签: python json kivy

我正在尝试将值存储在JsonStore文件中,但在单击按钮时会出现以下错误;

  

文件“C:\ utils \ kivy \ kivy \ kivy \ storage \ jsonstore.py”,第39行,   store_sync        open(self.filename,'w')为fd:PermissionError:[Errno 13]权限被拒绝:'storage.json'

# import Kivy
import kivy
import random
import json

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.storage.jsonstore import JsonStore

# my app
class MyApp(App):
# layout
    def build(self):
    # basic layout
        layout = BoxLayout(padding=10, orientation='vertical')
        self.store = JsonStore('storage.json')
        # widgets
        # button 1
        self.btn1 = Button(text="OK")
        self.btn1.bind(on_press=self.buttonClicked)
        layout.add_widget(self.btn1)
        # label 1
        self.lbl1 = Label(text="test")
        layout.add_widget(self.lbl1)
        # input 1
        self.txt1 = TextInput(text='', multiline=False)
        layout.add_widget(self.txt1)
        # return layout
        return layout




    # button click function
    def buttonClicked(self,btn):
        self.lbl1.text = "You wrote " + self.txt1.text
        self.store.put('myStorage1', value1=self.txt1.text, value2='test')
# run app
if __name__ == "__main__":
    MyApp().run()

1 个答案:

答案 0 :(得分:0)

使用Kivy的user_data_dir自动确定用户数据存储在不同设备上的位置,然后传递文件的完整路径进行存储。

from kivy.storage.jsonstore import JsonStore
from os.path import join

data_dir = App().user_data_dir
store = JsonStore(join(data_dir, 'storage.json'))