from tkinter import *
from tkinter import ttk
from time import strftime
import winsound
clock = Tk()
clock.title("WhatAClock")
clock.geometry("300x400")
notebook = ttk.Notebook()
tab1_timedate = Frame(notebook)
tab2_alarm = Frame(notebook)
tab3_timer = Frame(notebook)
notebook.add(tab1_timedate, text="Time and Date")
notebook.add(tab2_alarm, text="Alarm")
notebook.add(tab3_timer, text="Timer")
notebook.pack(expand=TRUE, fill="both")
def realtime():
time_str = strftime("%H:%M:%S")
l1_time_timedate.config(text= time_str)
l1_time_alarm.config(text= time_str)
clock.after(1000, realtime)
def alarm(alarm_set):
while True:
time_str_alarm = strftime("%H:%M:%S")
if time_str_alarm == alarm_set :
winsound.playsound("sound.wav",winsound.SND_ASYNC)
break
def set_alarm():
alarm_set = f"{user_h.get()}:{user_m.get()}:{user_s.get()}"
alarm(alarm_set)
l1_time_timedate = Label(tab1_timedate)
l1_time_alarm = Label(tab2_alarm)
l1_time_timedate.place(x=20, y=30)
l1_time_alarm.place(x=20, y=30)
user_h = StringVar()
user_m = StringVar()
user_s = StringVar()
entry_h = Entry(tab2_alarm, textvariable= user_h)
entry_m = Entry(tab2_alarm, textvariable= user_m)
entry_s = Entry(tab2_alarm, textvariable= user_s)
entry_h.place(x=100, y=30)
entry_m.place(x=130, y=30)
entry_s.place(x=160, y=30)
button_alarm = Button(tab2_alarm, command= set_alarm, text= "SET ALARM")
button_alarm.place(x=100, y=70)
realtime()
clock.mainloop()
"Total noob again, can t figure out why the button doesn t do what it s supposed to, any clue?
There are few issues:
the hour, minute and second of
alarm_set
are not zero padded. So if hour is 1 and minute is 2 and second is 3, thenalarm_set
will be"1:2:3"
. Howevertime_str_alarm
is something like"01:02:03"
, therefore the checking will not be what you want.don't use while loop inside
alarm()
as it will block tkintermainloop()
from processing pending events. Useafter()
like inrealtime()
winsound.playsound(...)
should bewinsound.PlaySound(...)
insteadBelow is the modified code: