[英]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.