|
| 1 | +fromtkinterimport* |
| 2 | +importdatetime |
| 3 | +importtime |
| 4 | +fromplaysoundimportplaysound |
| 5 | +fromtkinterimportmessagebox |
| 6 | +fromthreadingimport* |
| 7 | + |
| 8 | + |
| 9 | +root=Tk()# initializes tkinter to create display window |
| 10 | +root.geometry('450x250')# width and height of the window |
| 11 | +root.resizable(0,0)# sets fix size of window |
| 12 | +root.title(' Alarm Clock')# gives the window a title |
| 13 | + |
| 14 | + |
| 15 | +addTime=Label(root,fg="red",text="Hour Min Sec", |
| 16 | +font='arial 12 bold').place(x=210) |
| 17 | +setYourAlarm=Label(root,text="Set Time(24hrs): ", |
| 18 | +bg="grey",font="arial 11 bold").place(x=80,y=40) |
| 19 | +hour=StringVar() |
| 20 | +min=StringVar() |
| 21 | +sec=StringVar() |
| 22 | + |
| 23 | +# make the time input fields |
| 24 | +hourTime=Entry(root,textvariable=hour,relief=RAISED,width=4,font=(20)).place(x=210,y=40) |
| 25 | +minTime=Entry(root,textvariable=min,width=4,font=(20)).place(x=270,y=40) |
| 26 | +secTime=Entry(root,textvariable=sec,width=4,font=(20)).place(x=330,y=40) |
| 27 | + |
| 28 | + |
| 29 | +defstart_alarm(): |
| 30 | +t1=Thread(target=alarm) |
| 31 | +t1.start() |
| 32 | + |
| 33 | + |
| 34 | +defalarm(): |
| 35 | +whileTrue: |
| 36 | +set_alarm_time=f"{hour.get()}:{min.get()}:{sec.get()}" |
| 37 | +# sleep for 1s to update the time every second |
| 38 | +time.sleep(1) |
| 39 | +# Get current time |
| 40 | +actual_time=datetime.datetime.now().strftime("%H:%M:%S") |
| 41 | +FMT='%H:%M:%S' |
| 42 | +# get time remaining |
| 43 | +time_remaining=datetime.datetime.strptime( |
| 44 | +set_alarm_time,FMT)-datetime.datetime.strptime(actual_time,FMT) |
| 45 | +# displays current time |
| 46 | +CurrentLabel=Label( |
| 47 | +root,text=f'Current time:{actual_time}',fg='black') |
| 48 | +CurrentLabel.place(relx=0.2,rely=0.8,anchor=CENTER) |
| 49 | +# displays alarm time |
| 50 | +AlarmLabel=Label( |
| 51 | +root,text=f'Alarm time:{set_alarm_time}',fg='black') |
| 52 | +AlarmLabel.place(relx=0.2,rely=0.9,anchor=CENTER) |
| 53 | +# displays time remaining |
| 54 | +RemainingLabel=Label( |
| 55 | +root,text=f'Remaining time:{time_remaining}',fg='red') |
| 56 | +RemainingLabel.place(relx=0.7,rely=0.8,anchor=CENTER) |
| 57 | +# Check whether set alarm is equal to current time |
| 58 | +ifactual_time==set_alarm_time: |
| 59 | +# Playing sound |
| 60 | +playsound('audio.mp3') |
| 61 | +messagebox.showinfo("TIME'S UP!!!") |
| 62 | + |
| 63 | + |
| 64 | +# create a button to set the alarm |
| 65 | +submit=Button(root,text="Set Your Alarm",fg="red",width=20, |
| 66 | +command=start_alarm,font=("arial 20 bold")).pack(pady=80,padx=120) |
| 67 | +# run the program |
| 68 | +root.mainloop() |