繁体   English   中英

为什么我的if和elif语句一次只能用于一个按钮?

[英]Why does my if and elif statements only work for one button at a time?

我让程序显示存储在json文件中的按钮列表。 与当前时间time.time()和用户输入的delay相比,我具有每个按钮的存储时间honey

由于某些原因,条件条件仅在有一个按钮时才起作用。 一旦添加了其他按钮,程序将第一个按钮变为绿色以指示其为早,而新按钮变为黄色以指示其为早期。 新按钮变成红色后,第一个按钮也变成红色。

我已经为程序计时,并且程序在正确的时间运行。 为什么会有这个问题,我该如何解决? 码:

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.)

        return presentation

    def check_streak(self, dt):

        for child in reversed(self.root.screen_two.ids.streak_zone.children):
            honey = float(child.id)

            with open("streak.json", "r") as read_file:
                data = json.load(read_file)

            for value in data.values():
                if value['delay'] is not None:
                    delay = int(value['delay'])

                    if delay > time.time() < honey: # early (yellow)
                        child.background_normal = ''
                        child.background_color = [1, 1, 0, 1]

                    elif delay > time.time() > honey: # on time (green)
                        child.background_normal = ''
                        child.background_color = [0, 1, 0, 1]


                    elif delay < time.time() > honey: # late (red)
                        child.background_normal = ''
                        child.background_color = [1, 0, 0, 1]

def display_btn(self):
        # display the names of the streaks in a list on PageTwo
        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=str(value['delta']), text=value['action'],
                                            on_press=self.third_screen, size=(400,50),
                                            size_hint=(None,None))
                self.root.screen_two.ids.streak_zone.add_widget(streak_button)

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

            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)

streak.json文件: {"one": {"action": "one", "delay": 1557095861.2131674, "seconds": 60, "score": 0, "delta": 1557095801.2131674}, "two": {"action": "two", "delay": 1557096131.7338686, "seconds": 60, "score": 0, "delta": 1557096071.7338686}}

它仅适用于一个按钮,因为if...elif语句在for循环之外。

if...elif块移至if value['delay'] is not None: for循环内的块。

片段

def check_streak(self, dt):

    for child in reversed(self.root.screen_two.ids.streak_zone.children):
        honey = float(child.id)

        with open("streak.json", "r") as read_file:
            data = json.load(read_file)

        for value in data.values():
            if value['delay'] is not None:
                delay = int(value['delay'])

                # fix for later
                if delay > time.time() < honey: # early (yellow)
                    child.background_normal = ''
                    child.background_color = [1, 1, 0, 1]

                elif delay > time.time() > honey: # on time (green)
                    child.background_normal = ''
                    child.background_color = [0, 1, 0, 1]

                elif delay < time.time() > honey: # late (red)
                    child.background_normal = ''
                    child.background_color = [1, 0, 0, 1]

问题是有两个循环导致我的数据不匹配。 我需要能够遍历按钮对象并仅对每个对象使用正确的json文件字典条目,或者遍历json文件并仅修改相应的子对象。 我决定使用前者。

我设置了代码,以便在streak_zone name = child.text中的每个孩子name = child.text

然后,我将json文件中的每个键与按钮的名称(也就是child

如果键的名称等于name那么我们得到嵌套键delay的值

新代码:

def check_streak(self, dt):

        for child in reversed(self.root.screen_two.ids.streak_zone.children):
            honey = float(child.id)
            name = child.text


            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')


                if delay > time.time() < honey: # early (yellow)
                    child.background_normal = ''
                    child.background_color = [1, 1, 0, 1]

                elif delay > time.time() > honey: # on time (green)
                    child.background_normal = ''
                    child.background_color = [0, 1, 0, 1]


                elif delay < time.time() > honey: # late (red)
                    child.background_normal = ''
                    child.background_color = [1, 0, 0, 1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM