Когда я нажимаю кнопку, вызывается функция check_streak()
. Я хочу только сравнить это время кнопок ака «мед» с time.time()
. Вместо того, чтобы печатать ответ на одну кнопку «honey», сравниваются все кнопки «honey», и для каждой из них печатается ответ. Поскольку кнопка создается в цикле for, я не уверен, как я могу указать, что хочу сравнивать только эту конкретную кнопку. Как бы я это исправить? код:
class MainApp(App):
def build(self): # build() returns an instance
self.store = JsonStore("streak.json") # file that stores the streaks:
return presentation
# Gets the value of delta (code I may never understand lol)
def item_generator(self, json_input, lookup_key):
if isinstance(json_input, dict):
for k, v in json_input.items():
if k == lookup_key:
yield v
else:
yield from self.item_generator(v, lookup_key)
elif isinstance(json_input, list):
for item in json_input:
yield from self.item_generator(item, lookup_key)
def check_streak(self, instance):
with open("streak.json", "r") as read_file:
data = json.load(read_file)
values = self.item_generator(data, 'delta')
for honey in values:
if honey > time.time():
print("early")
if honey == time.time():
print("on time")
if honey < time.time():
print("late")
def display_btn(self):
# display the names of the streaks in a list on PageTwo
for key in self.store:
streak_button = Button(text=key, on_press=self.check_streak)
self.root.screen_two.ids.streak_zone.add_widget(streak_button)
В настоящее время у меня есть 3 кнопки в программе. Когда я нажимаю кнопку, я ожидаю печатать «рано», но вместо этого я печатаю «рано рано рано», потому что есть 3 кнопки.