2

I'm using appjar to create a python app that includes the feature to set an alarm to go off at a certain time/date. However, based on my simple implementation, when calling the alarm function in the code the application will just infinitely wait for the alarm to go off before allowing anything else to happen.

I want to be able to navigate around the app without it having to wait on the alarm. What is the best way to go about doing this?

Here is my alarm code:

def setAlarm(year, month, day, hour, minute, second):

    now = datetime.datetime.now()
    date = datetime.date(year, month, day)
    time = datetime.time(hour, minute, second)
    alarmTime = datetime.datetime(year, month, day, hour, minute, second)

    while now < alarmTime:
        print("not yet")

    mixer.music.load('Alarm.wav')
    mixer.music.play(-1)

    sound = True

    while(sound):
        print("Alarm")
RK1
  • 2,384
  • 1
  • 19
  • 36

2 Answers2

1

Check out Python Timers. If this doesn't work, your solution will involve some kind of multi-threading or multiprocessing such that you can have two paths of execution running at the same time.

You should be able to set a timer for a short amount of time. When the timer goes off, have the code that it runs when it does check for the condition you're waiting for. If that condition still isn't met, have that code fire off a new timer to wait some more. When you create and start a timer, control returns immediately to your code, so you can go on and do other things.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
0

I have gone through the same problem then I solved it in my way.

import time
from playsound import playsound
import threading
from datetime import datetime
import playsound

#taking input from user
alarmH = 3
alarmM = 10
amPm = 'am'

print("Weating for the alarm",alarmH,alarmM,amPm)
if (amPm == "pm"):
     alarmH = alarmH + 12

#Current Date Time
now = datetime.now()

#desired alarm time
later = datetime(2020,5,1,alarmH,alarmM,0)

#calculating the difference between two time
difference = (later - now)

#difference in seconds
total_sec=difference.total_seconds() 

def alarm_func():
    playsound.playsound('audio/alarm.mp3', True)

timer = threading.Timer(total_sec, alarm_func)
timer.start()
Md.Rakibuz Sultan
  • 759
  • 1
  • 8
  • 13