alarm_clock.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Import Required Library
  2. from tkinter import *
  3. import datetime
  4. import time
  5. import winsound
  6. from threading import *
  7. # Create Object
  8. root = Tk()
  9. # Set geometry
  10. root.geometry("400x200")
  11. # Use Threading
  12. def Threading():
  13. t1=Thread(target=alarm)
  14. t1.start()
  15. def alarm():
  16. # Infinite Loop
  17. while True:
  18. # Set Alarm
  19. set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
  20. # Wait for one seconds
  21. time.sleep(1)
  22. # Get current time
  23. current_time = datetime.datetime.now().strftime("%H:%M:%S")
  24. print(current_time,set_alarm_time)
  25. # Check whether set alarm is equal to current time or not
  26. if current_time == set_alarm_time:
  27. print("Time to Wake up")
  28. # Playing sound
  29. winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
  30. # Add Labels, Frame, Button, Optionmenus
  31. Label(root,text="Alarm Clock",font=("Helvetica 20 bold"),fg="red").pack(pady=10)
  32. Label(root,text="Set Time",font=("Helvetica 15 bold")).pack()
  33. frame = Frame(root)
  34. frame.pack()
  35. hour = StringVar(root)
  36. hours = ('00', '01', '02', '03', '04', '05', '06', '07',
  37. '08', '09', '10', '11', '12', '13', '14', '15',
  38. '16', '17', '18', '19', '20', '21', '22', '23', '24'
  39. )
  40. hour.set(hours[0])
  41. hrs = OptionMenu(frame, hour, *hours)
  42. hrs.pack(side=LEFT)
  43. minute = StringVar(root)
  44. minutes = ('00', '01', '02', '03', '04', '05', '06', '07',
  45. '08', '09', '10', '11', '12', '13', '14', '15',
  46. '16', '17', '18', '19', '20', '21', '22', '23',
  47. '24', '25', '26', '27', '28', '29', '30', '31',
  48. '32', '33', '34', '35', '36', '37', '38', '39',
  49. '40', '41', '42', '43', '44', '45', '46', '47',
  50. '48', '49', '50', '51', '52', '53', '54', '55',
  51. '56', '57', '58', '59', '60')
  52. minute.set(minutes[0])
  53. mins = OptionMenu(frame, minute, *minutes)
  54. mins.pack(side=LEFT)
  55. second = StringVar(root)
  56. seconds = ('00', '01', '02', '03', '04', '05', '06', '07',
  57. '08', '09', '10', '11', '12', '13', '14', '15',
  58. '16', '17', '18', '19', '20', '21', '22', '23',
  59. '24', '25', '26', '27', '28', '29', '30', '31',
  60. '32', '33', '34', '35', '36', '37', '38', '39',
  61. '40', '41', '42', '43', '44', '45', '46', '47',
  62. '48', '49', '50', '51', '52', '53', '54', '55',
  63. '56', '57', '58', '59', '60')
  64. second.set(seconds[0])
  65. secs = OptionMenu(frame, second, *seconds)
  66. secs.pack(side=LEFT)
  67. Button(root,text="Set Alarm",font=("Helvetica 15"),command=Threading).pack(pady=20)
  68. # Execute Tkinter
  69. root.mainloop()