我的kivy应用程序使用按钮,这些按钮与单独json文件中的json元素相对应
这将是按钮Eight
"Eight": {
"action": "Eight",
"delay": 1.55975283E9,
"seconds": 0,
"score": 0,
"delta": 1.55974682E9,
"grace_sec": 6000
}
当我按下按钮时,每次分数都会增加1。但是,当我添加另一个按钮时,分数重置为0
我真的不确定它是什么,因为我的代码曾经没有这样做。我一定是在不知不觉中改变了什么。我相信这个问题可能与kivy的store
函数有关,但我不确定
我将包括我认为可能影响json文件的所有代码
class MainApp(App):
def build(self): # build() returns an instance
self.store = JsonStore("streak.json") # file that stores the streaks:
Clock.schedule_interval(self.check_streak, 1/30.) # used to call functions dynamicaly
Clock.schedule_interval(self.score_gone, 1/30.)
Clock.schedule_interval(self.update_high, 1/30.)
return presentation
这个函数更长,但我只包含了一个分数为1的部分
def check_streak(self, dt):
for child in reversed(self.root.screen_two.ids.streak_zone.children):
name = child.id
with open("streak.json", "r") as read_file:
data = json.load(read_file)
for key in data.keys():
if key == name:
delay = data.get(key, {}).get('delay') # get value of nested key 'delay'
self.honey = data[key]['delta']
float(self.honey)
...
elif delay > time.time() > self.honey: # on time (green)
child.background_normal = ''
child.background_color = [0, 1, 0, .95]
child.unbind(on_press=self.early_click)
child.bind(on_press=self.add_score)
child.bind(on_press=self.display_streak)
child.bind(on_press=self.draw_streak)
child.unbind(on_press=self.late_click)
# add 1 to score and store in json file
def add_score(self, obj):
name = obj.id
with open("streak.json", "r") as file:
read = json.load(file)
for key in read.keys():
if key == name:
with open("streak.json", "r+") as f:
data = json.load(f)
data[key]['score']+=1
grace_sec = data.get(key, {}).get('grace_sec')
new_delay = time.time() + grace_sec
data[key]['delay'] = new_delay
seconds = data.get(key, {}).get('seconds')
new_delta = time.time() + seconds
data[key]['delta'] = new_delta
f.seek(0)
json.dump(data, f, indent=4)
f.truncate()
我之所以包括这个,是因为它是主应用程序的一个实例,并且总是因为Clock
函数而被调用,但我认为这不是问题的原因,所以如果是这样的话,您可以忽略它
# changes score to 0 and stores in json file
def score_gone(self, dt):
for child in self.root.screen_two.ids.streak_zone.children:
name = child.id
color = child.background_color
with open("streak.json", "r") as file:
read = json.load(file)
if color == [1, 0, 0, .95]: # red
if read[name]['score'] != 0: #stops slow down from Clock
with open("streak.json", "r+") as f: # fix score not reseting to 0
data = json.load(f)
data[name]['score'] = 0
f.seek(0)
json.dump(data, f, indent=4)
f.truncate()
elif read[name]['score'] == 0: #stops slow down from Clock
pass
这是从上一页中输入的文本创建条纹的函数
# creates the Streak object
def create(self):
...
# store streak attributes inside "streak.json"
self.store.put(self.streak.action, action=self.streak.action,
delay=grace, seconds=total,
score=0, delta=self.count, grace_sec=grace_sec)
self.change_screen(self) # changes to screen that displays buttons
此功能显示按钮
# display the names of the streaks in a list on PageTwo
def display_btn(self):
no_data = "You have no stored streaks!"
popup_2 = Popup(title="No Streaks!", content=Label(text=no_data),
size_hint=(None, None), size=(300, 100))
with open("streak.json", "r") as read_file:
data = json.load(read_file)
for value in data.values():
if value['delta'] is not None:
print(f"action={value['action']}, delta={value['delta']}, grace={value['delay']}")
streak_button = StreakButton(id=(value['action']), text=(value['action'] + " " + "[" + str(value['score']) + "]"),
color=(0,0,0,1), size=(400, 50),
size_hint=(None, None))
self.root.screen_two.ids.streak_zone.add_widget(streak_button)
...
离开页面时删除所有按钮小部件,进入页面时调用display_btn
。令我感到奇怪的是,即使json文件更新为新的分数,程序在添加新按钮之前仍会记住分数值。如果我给按钮Eight
加上1分,那么就退出程序;应用程序将记住该值为1。因此,如果我添加了一个值2,然后添加了一个新按钮,Eight
的得分值将重置为1,因为这是我关闭应用程序之前的值
编辑
这是create
中的所有代码,用于从页面内部输入的文本收集数据。您看到的数学只是将输入转换为秒,以便稍后的函数比较时间(我不确定是否有必要)
def create(self):
obj = self.root.get_screen('one') # get info from ScreenOne
self.streak = Streak(obj.ids.action_entry.text, obj.ids.delay_entry.text,
obj.ids.day_entry.text, obj.ids.hour_entry.text,
obj.ids.minute_entry.text)
empty_error = "Make sure to fill out all boxes!" # not in use yet
popup = Popup(title="Not filled", content=Label(text=empty_error),
size_hint=(None, None), size=(300, 100))
# error handling and calculating total seconds
parsed = False
try:
total = ((int(self.streak.day) * 86400) + (int(self.streak.hour) * 3600) +
(int(self.streak.minute) * 60)) # convert into seconds
self.current_time = time.time()
self.count = self.current_time + total
grace = (int(self.streak.delay) * 60) + self.count # aka delay
grace_sec = (int(self.streak.delay) * 60) + total
parsed = True
# delete later just used to test
print("[seconds:", total, ']', "[action:", self.streak.action, ']',
"[grace:", grace, ']')
# store streak attributes inside "streak.json"
self.store.put(self.streak.action, action=self.streak.action,
delay=grace, seconds=total,
score=0, delta=self.count, grace_sec=grace_sec)
self.change_screen(self)
except ValueError as error:
popup.open()
创建使用Streak的实例
class Streak():
def __init__(self, action, delay, day, hour, minute, score=0, delta=0):
self.action = action
self.delay = delay
self.day = day
self.hour = hour
self.minute = minute
self.score = score
self.delta = delta
当您添加新信息时,json会重置,您需要在添加新信息之前添加新信息,我认为这是导致问题的原因
正在重置JSON文件,因为
self.store
在build()
方法中填充了add之前的图像。将self.store = JsonStore("streak.json")
添加到create()
方法中片段
相关问题 更多 >
编程相关推荐