Kivy storage JsonStore:无法使异步保存工作(async.put 方法)



我做了一些小的kivy应用程序,可以在光盘上显示json文件的内容。我做了一些按钮来增加 json 文件中的值,这工作正常。标签显示文件的更新值。问题是我想以异步方式做同样的事情(出于经济原因)。不幸的是,我无法弄清楚这一点。关于存储模块的文档([http://kivy.org/docs/api-kivy.storage.html][1])非常简短,我没有找到任何示例。这是我的代码,注释掉的部分需要修复,任何其他关于存储的提示都非常受欢迎!

import kivy
kivy.require('1.9.1')
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.storage.jsonstore import JsonStore
from kivy.app import App
from kivy.clock import Clock
from functools import partial
# The initial content of the json file
initial_user_stats = {"words":{"word1":{"tries":0,"succes":0},"word2":{"tries":0,"succes":0}}}
#let's imagine John logged in and a json file is created on disc
user_stats = JsonStore ("john.json")
user_stats["words"]=initial_user_stats["words"]

class MyQuizScreen(BoxLayout):
    def __init__(self, **kwargs):
        super(MyQuizScreen, self).__init__(**kwargs)
        self.orientation = "vertical"
        self.spacing = "4dp"
        self.content_label = Label(halign = "center", font_size = "16dp", size_hint_y = .25) 
        self.add_widget(self.content_label)
        grid = GridLayout(cols = 2)
        grid.add_widget(Button(text="increase 'tries'nof word 1n(synchronous)", on_press = partial(self.sync_button_callback, "word1", "tries")))
        grid.add_widget(Button(text="increase 'succes'nof word 1n(synchronous)", on_press = partial(self.sync_button_callback, "word1", "succes")))
        grid.add_widget(Button(text="increase 'tries'nof word 2n(synchronous)", on_press = partial(self.sync_button_callback, "word2", "tries")))
        grid.add_widget(Button(text="increase 'succes'nof word 2n(synchronous)", on_press = partial(self.sync_button_callback, "word2", "succes")))
        #The next lines were not working, I couldn't figure out the async way of doing things (now it works in 1.9.1)
        grid.add_widget(Button(text="increase 'tries'nof word 1n(a_synchronous)",on_press = partial(self.async_button_callback, "word1", "tries")))
        grid.add_widget(Button(text="increase 'succes'nof word 1n(a_synchronous)", on_press = partial(self.async_button_callback, "word1", "succes")))
        grid.add_widget(Button(text="increase 'tries'nof word 2n(na_synchronous)", on_press = partial(self.async_button_callback, "word2", "tries")))
        grid.add_widget(Button(text="increase 'succes'nof word 2n(a_synchronous)", on_press = partial(self.async_button_callback, "word2", "succes")))
        self.add_widget(grid)
        Clock.schedule_interval(self.update_text_label, .2)
    def sync_button_callback(self, key, subkey, button):
        user_stats["words"][key][subkey] += 1
        user_stats.put("words", **user_stats["words"]) 
    def async_button_callback(self, key, subkey, button):
        # Here the error occured
        user_stats["words"][key][subkey] += 1
        user_stats.async_put(self.my_async_callback, "words", **user_stats["words"])
    def my_async_callback(self, store, key, result):
        print "store:", store
        print "key:", key
        print "result", result
    def update_text_label(self,*args):
        stats_dict = dict (user_stats)
        x = stats_dict.items()
        self.content_label.text = "The json file on disc:nn" + str(x)
        self.content_label.text_size = self.content_label.size

class QuizApp(App):
    def build(self):
        root = MyQuizScreen()
        return root
if __name__ == '__main__':
    QuizApp().run()

根据 irc 聊天上的开发人员的说法,这是 kivy 1.9.0 中的一个错误并且只应使用同步方法(2015 年 4 月 13 日)。所以上面的代码正在工作(因为异步方法被注释掉了)。

最新更新