【问题标题】:Kivy storage JsonStore: can't make async saving to work (the async.put method)Kivy 存储 JsonStore:无法使异步保存工作(async.put 方法)
【发布时间】:2015-06-18 22:24:03
【问题描述】:

我制作了一些小型 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 1\n(synchronous)", on_press = partial(self.sync_button_callback, "word1", "tries")))
        grid.add_widget(Button(text="increase 'succes'\nof word 1\n(synchronous)", on_press = partial(self.sync_button_callback, "word1", "succes")))
        grid.add_widget(Button(text="increase 'tries'\nof word 2\n(synchronous)", on_press = partial(self.sync_button_callback, "word2", "tries")))
        grid.add_widget(Button(text="increase 'succes'\nof word 2\n(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 1\n(a_synchronous)",on_press = partial(self.async_button_callback, "word1", "tries")))
        grid.add_widget(Button(text="increase 'succes'\nof word 1\n(a_synchronous)", on_press = partial(self.async_button_callback, "word1", "succes")))
        grid.add_widget(Button(text="increase 'tries'\nof word 2\n(na_synchronous)", on_press = partial(self.async_button_callback, "word2", "tries")))
        grid.add_widget(Button(text="increase 'succes'\nof word 2\n(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:\n\n" + 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()

【问题讨论】:

    标签: asynchronous kivy jsonstore


    【解决方案1】:

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

    【讨论】:

    • 现在已经在 GitHub(master 分支)的开发版本中修复了。
    • 同步和异步适用于 kivy 1.9.1(目前为 dev)
    猜你喜欢
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多