Advertisement
ZEdKasat

Python Alarm Clock

Jul 28th, 2021
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. import tkinter as tk
  2. import datetime as dt
  3. import playsound as ps
  4.  
  5. def checkTime(hr, mn):
  6.     chr = dt.datetime.now().hour
  7.     cmn = dt.datetime.now().minute
  8.    
  9.     if chr == hr and cmn == mn:
  10.         print("Alarm going off!")
  11.         ps.playsound("alarm.mp3", block=False)
  12.     else:
  13.         window.after(1000, lambda: checkTime(hr, mn))
  14.  
  15. def setAlarm():
  16.     hr = ent_hr.get()
  17.     mn = ent_min.get()
  18.     print(f"Alarm set for {hr}:{mn}.")
  19.     checkTime(int(hr), int(mn))
  20.  
  21. window = tk.Tk()
  22.  
  23. window.title("Alarm Clock")
  24.  
  25. lbl_hr = tk.Label(text = "Hour: ")
  26. lbl_min = tk.Label(text = "Minute: ")
  27.  
  28. ent_hr = tk.Entry()
  29. ent_min = tk.Entry()
  30.  
  31. btn_set = tk.Button(text="Set Alarm", command=setAlarm)
  32.  
  33. lbl_hr.grid(row = 0, column = 0, padx=(30, 2), pady=(15, 2))
  34. lbl_min.grid(row = 1, column = 0, padx=(30, 2), pady=(2, 30))
  35. ent_hr.grid(row = 0, column = 1, padx = (2, 30), pady=(15,2))
  36. ent_min.grid(row = 1, column = 1, padx=(2, 30), pady=(2, 5))
  37. btn_set.grid(row = 2, column = 0, columnspan = 2, pady=(0, 15))
  38.  
  39. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement