我做了一些小型的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()
答案 0 :(得分:1)
根据irc chat的开发人员的说法,这是kivy 1.9.0中的一个错误 并且只应使用同步方法(2015年4月13日)。所以上面的代码是有效的(因为异步方法被注释掉了。)