There are a few things to work here.
The most important is where you are testing the for the alarm.
The code to check if an alarm time has been reached needs to go into your update_time method. You are entering this method once per second to update the time, you also need to check if the alarm time has been reached here.
To access the alarm times from the update_time method you would do the following:
class MyClock(Label):
current_time = StringProperty(strftime("%I:%M:%S %p"))
def __init__(self, **kwargs):
Clock.schedule_interval(self.update_time, 1)
super().__init__(**kwargs)
def update_time(self, dt):
self.current_time = strftime("%I:%M:%S %p")
app = App.get_running_app()
t1 = app.root.ids.sm.get_screen(‘edit_1_page’).alarm(self.current_time)
# the alarm current code needs to change – you are passing in the current time, the alarm knows the alarm time.
# make sure the format of the current_time and the alarm are the same for the comparison.
I also recommend you crete some content for your popup, at least a button that can dismiss the popup.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/377185b6-effc-4712-8327-908ffde32c5co%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f249724.1c69fb81.a24e9.6baaSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
formatted_alarm = self.alarm_time.split(":")
Creates a list that contains the hours, minutes and seconds.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV45JR_5si%2BpkXgcthtyz2-6quGYF7z4%3Dw2k40qRtqZ3mJA%40mail.gmail.com.
The code below works. Here is a summary of the most significant changes.
def alarm(self, current_time):
am_pm = {'down': 'AM', 'normal': 'PM'}[self.alarm_am_state]
a_time = f'{self.alarm_time}:00 {am_pm}'
if a_time[1] == ':': # if the : is the second char,a leading zero is missing, insert it.
a_time = '0' + a_time
print(f'a: {a_time} ct: {current_time}') # test to watch the time...
if a_time == current_time:
popup = Popup(title='Alarm 1', content=Label(text=self.alarm_time), size_hint=(None, None), size=(400, 400))
popup.open()
sound = SoundLoader.load('Ring05.wav')
sound.play()
The method alarm() is called every second by the update_time() method.
It constructs a string a_time that matches the formatting of the current_time. The time can be entered with or without a leading zero. If a ‘:’ is the second character in the string, a ‘0’ is prepended, to ensure the formatting matches.
The alarm time and the current time are printed to support debug. The Popup comes up and the sound plays.
The set_alarm() method was simplified.
You might want to consider creating a text input widget that only allows a valid time to be entered.
Full code with update below:
-----
# am_pm = {'down': 'AM', 'normal': 'PM'}[self.alarm_am_state]
self.alarm_time = self.ids.alarmtime.text
# formatted_alarm = self.alarm_time.split(":")
#
# self.passed_alarm = formatted_alarm.strftime("%I %M %S")
# alarm_1 = self.alarm_time
# self.alarm(alarm_1)
with open("alarm1_details.txt", "w") as f:
f.write(f"{self.ids.alarmtime.text},{self.alarm_am_state},{self.alarm_pm_state}")
# print(f'Alarm time set: {self.alarm_time}')
def alarm(self, current_time):
am_pm = {'down': 'AM', 'normal': 'PM'}[self.alarm_am_state]
a_time = f'{self.alarm_time}:00 {am_pm}'
if a_time[1] == ':': # if the : is the second char,a leading zero is missing, insert it.
a_time = '0' + a_time
print(f'a: {a_time} ct: {current_time}') # test to watch the time...
if a_time == current_time:
popup = Popup(title='Alarm 1', content=Label(text=self.alarm_time), size_hint=(None, None), size=(400, 400))
popup.open()
sound = SoundLoader.load('Ring05.wav')
sound.play()
class MyButton(ToggleButtonBehavior, Label, Image):
def __init__(self, **kwargs):
super(MyButton, self).__init__(**kwargs)
self.source = 'atlas://data/images/defaulttheme/checkbox_off'
def on_state(self, widget, value):
if value == 'down':
self.source = 'atlas://data/images/defaulttheme/checkbox_on'
else:
self.source = 'atlas://data/images/defaulttheme/checkbox_off'
class MainApp(App):
def build(self):
return Builder.load_string(kv)
if __name__ == "__main__":
MainApp().run()
Sent: Saturday, August 1, 2020 7:07 PM
To: kivy-...@googlegroups.com
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV45JR_5si%2BpkXgcthtyz2-6quGYF7z4%3Dw2k40qRtqZ3mJA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5f2628df.1c69fb81.a3572.c2f1SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
The current time is formatted as:
09:34:10 PM
So we need to match that.
You were making changes to set alarm, creating an attribute that would hold the time in a format to compare current time:
formatted_alarm = self.alarm_time.split(":")
This will create a list of items separated at the ‘:’ or using 09:34:10 PM’
[‘09’, ‘34’, ’10 PM] I’m not sure what you were trying here, perhaps you were going to convert hours and minutes to ints for the compare operation.
self.passed_alarm = formatted_alarm.strftime("%I %M %S")
The line above was the line that was failing – I’m not sure what you were trying to do here.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG4TV449M3hXcXebm19Un_FXv7Hs2pdjYoH8m78j11XaGtbZRA%40mail.gmail.com.