使用函数并在每个“指定的时间间隔”内为该函数注册一个回调,而不是while True:
循环。这是在下面的代码中完成的:
def wait_for_alarm():
after = r.after(1000,wait_for_alarm) # 1000 refers to 1 second
if time.localtime().tm_hour == hr and time.localtime().tm_min == mn and time.localtime().tm_sec == sc:
print(message)
alarm_Sound()
mixer.music.play()
,
首先,您应该使用全局变量来保留Entry
中的值,并稍后在检查时间的函数中使用它们。没有警报时间时,该函数应使用after
重新运行。它还应该跳过after()
以停止检查时间。
def display_output():
global int_hour
global int_minute
global int_second
hour = entry_hour.get()
int_hour = int(hour)
minute = entry_minute.get()
int_minute = int(minute)
second = entry_second.get()
int_second = int(second)
text_label['text'] = 'Alarm has been set for {}:{}:{}.'.format(hour,minute,second)
# start checking time
check_time()
def check_time():
dt = time.localtime()
if (dt.tm_hour == int_hour and dt.tm_min == int_minute and dt.tm_sec == int_second):
alarm_sound()
mixer.music.play()
else:
r.after(1000,check_time) # check it again after 1000ms but only if it doesn't play sound
代码将需要其他全局变量来控制在停止警报以及尝试再次设置警报时是否正在运行警报。
import time
from datetime import datetime,date
import tkinter as tk
from pygame import mixer
# --- functions ---
def alarm_sound():
global running_alarm
running_alarm = True
mixer.music.load('Wecker-sound.mp3')
def end():
global running_alarm
if running_alarm:
running_alarm = False
mixer.music.stop()
text_label['text'] = ''
def display_output():
global int_hour
global int_minute
global int_second
hour = entry_hour.get()
int_hour = int(hour)
minute = entry_minute.get()
int_minute = int(minute)
second = entry_second.get()
int_second = int(second)
confirmation_message = ('Alarm has been set for {}:{}:{}.'.format(hour,second))
text_label['text'] = confirmation_message
check_time()
def check_time():
dt = time.localtime()
if (dt.tm_hour == int_hour and dt.tm_min == int_minute and dt.tm_sec == int_second):
checking_time = False # stop l
alarm_sound()
mixer.music.play()
else:
r.after(1000,check_time)
# --- main ---
running_alarm = False # default value at start
mixer.init()
r = tk.Tk()
r.title('Smart Watch')
message = tk.Label(r,text='What is the alarm message you\'d like to receive when the alarm goes off?',font=('Roboto-regular',12),fg='black')
message.pack()
entry_message = tk.Entry(r,width=45)
entry_message.pack()
enter_hour = tk.Label(r,text='Enter the hour for the alarm to go off: ',fg='black')
enter_hour.pack()
entry_hour = tk.Entry(r,width=10)
entry_hour.pack()
enter_minute = tk.Label(r,text='Enter the minute for the alarm to go off: ',fg='black')
enter_minute.pack()
entry_minute = tk.Entry(r,width=10)
entry_minute.pack()
enter_second = tk.Label(r,text='Enter the second for the alarm to go off: ',fg='black')
enter_second.pack()
entry_second = tk.Entry(r,width=10)
entry_second.pack()
text_label = tk.Label(r,12,'bold'),fg='tomato')
text_label.pack()
submit_button = tk.Button(r,text='Submit',fg='black',width=30,height=2,relief='groove',cursor='hand2',command=display_output)
submit_button.pack()
snooze_button = tk.Button(r,text='Snooze alarm',command=end)
snooze_button.pack()
r.mainloop()
本文链接:https://www.f2er.com/3118539.html