是什么导致添加kivy小部件后重置json文件?

时间:2019-06-05 15:43:07

标签: python json kivy

我的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,因为这是我关闭该应用程序之前的值。

EDIT

这是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()

create使用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

2 个答案:

答案 0 :(得分:1)

由于self.store方法在build()方法中填充了添加前的图像,因此重置了JSON文件。将self.store = JsonStore("streak.json")添加到create()方法中。

摘要

def create(self):
    self.store = JsonStore("streak.json") 

答案 1 :(得分:0)

当您添加新信息时json重置,您需要在添加新信息之前添加新信息,我认为这会导致问题

相关问题