Create an Advanced Python Alarm Clock with Animation
ฝัง
- เผยแพร่เมื่อ 10 ก.ย. 2024
- In this video, I'll show you how to create an advanced alarm clock in Python that not only rings but also displays a cool text animation. Follow along as we build this step-by-step, and stay tuned for more advanced features in future videos!
Don't forget to like, share, and subscribe for more awesome coding tutorials!
import datetime
import time
import pygame
import itertools
def animate_alarm():
animation = itertools.cycle(['|', '/', '-', '\\'])
while True:
print(f"
Alarm ringing! {next(animation)}", end='')
time.sleep(0.1)
def set_alarm(alarm_time):
pygame.mixer.init() # Initialize pygame mixer
pygame.mixer.music.load("morning_alarm_fobo.mp3") # Load the sound file
print(f"Alarm set for {alarm_time}")
while True:
current_time = datetime.datetime.now().strftime("%H:%M:%S")
print(f"
Current time: {current_time}", end="")
if current_time == alarm_time:
print("
Alarm ringing!")
pygame.mixer.music.play() # Play the alarm sound
animate_alarm() # Start the animation
while pygame.mixer.music.get_busy():
time.sleep(1)
break
time.sleep(1)
def main():
alarm_time = input("Enter the alarm time (HH:MM:SS): ").strip()
try:
datetime.datetime.strptime(alarm_time, "%H:%M:%S")
except ValueError:
print("Invalid time format. Please enter the time in HH:MM:SS format.")
return
set_alarm(alarm_time)
if _name_ == "__main__":
main()