现在让我们逐个解释每个代码块的作用。
from tkinter import *import datetimeimport timeimport winsoundfrom threading import *
首先,我们导入了 tkinter 库、datetime、time 库、winsound 库以及 threading 库。
root = tk()root.geometry("400x200")
创建一个名为 root 的窗口,并设置窗口大小为 400x200。
def threading(): t1=thread(target=alarm) t1.start()
定义一个函数 threading(),它将创建一个名为 t1 的新线程,该线程的目标是运行 alarm() 函数,并启动该线程。
def alarm(): while true: set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}" time.sleep(1) current_time = datetime.datetime.now().strftime("%h:%m:%s") print(current_time,set_alarm_time) if current_time == set_alarm_time: print("time to wake up") winsound.playsound("sound.wav",winsound.snd_async)
这个函数 alarm() 是在一个单独的线程中运行的,该线程每隔一秒钟检查一次当前时间是否等于设置的时间。如果当前时间和设置时间相等,就会调用 winsound.playsound() 播放一个音乐文件,表示时间到了。
在函数开头,首先通过 hour.get()、minute.get() 和 second.get() 获取用户在界面上设置的小时、分钟和秒数,然后将它们组合成一个字符串 set_alarm_time。接下来,使用 time.sleep() 让线程休眠 1 秒钟,这样每隔 1 秒钟就会检查一次当前时间是否等于设置时间。
然后,通过 datetime.datetime.now().strftime("%h:%m:%s") 获取当前时间,并将其格式化为小时、分钟和秒数的字符串 current_time。最后,如果 current_time 等于 set_alarm_time,就会打印一条消息并调用 winsound.playsound() 播放一个音乐文件。
label(root,text="alarm clock",font=("helvetica 20 bold"),fg="red").pack(pady=10)label(root,text="set time",font=("helvetica 15 bold")).pack()frame = frame(root)frame.pack()hour = stringvar(root)hours = ('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24' )hour.set(hours[0])hrs = optionmenu(frame, hour, *hours)hrs.pack(side=left)minute = stringvar(root)minutes = ('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60')minute.set(minutes[0])mins = optionmenu(frame, minute, *minutes)mins.pack(side=left)second = stringvar(root)seconds = ('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60')second.set(seconds[0])secs = optionmenu(frame, second, *seconds)secs.pack(side=left)button(root,text="set alarm",font=("helvetica 15"),command=threading).pack(pady=20)
这段代码用于添加标签、框架、按钮和选项菜单。先创建了一个框架 frame,然后创建了三个 stringvar 类型的变量 hour,minute,second 来存储用户选择的小时、分钟和秒数。然后定义了三个元组 hours,minutes,seconds 分别存储 00 到 60 的数字字符串,其中的 0~9 数字前面补零,保证格式一致。接着用 optionmenu 创建了三个选项菜单,分别用来选择小时、分钟和秒数。
综上所述,本文提供了一个基本的闹钟应用程序,可以通过gui界面设置和启动。当闹钟时间到达时,应用程序将播放声音,以提醒用户。
附完整源码:
# 导入所需库from tkinter import *import datetimeimport timeimport winsoundfrom threading import *# 创建对象root = tk()# 设置几何体root.geometry("400x200")# 使用线程def threading(): t1=thread(target=alarm) t1.start()def alarm(): # 无限循环 while true: # 设置警报 set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}" # 等待一秒钟 time.sleep(1) # 获取当前时间 current_time = datetime.datetime.now().strftime("%h:%m:%s") print(current_time,set_alarm_time) # 检查设置的报警是否等于当前时间 if current_time == set_alarm_time: print("time to wake up") # 播放声音 winsound.playsound("sound.wav",winsound.snd_async)# 添加标签、框架、按钮、选项菜单label(root,text="alarm clock",font=("helvetica 20 bold"),fg="red").pack(pady=10)label(root,text="set time",font=("helvetica 15 bold")).pack()frame = frame(root)frame.pack()hour = stringvar(root)hours = ('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24' )hour.set(hours[0])hrs = optionmenu(frame, hour, *hours)hrs.pack(side=left)minute = stringvar(root)minutes = ('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60')minute.set(minutes[0])mins = optionmenu(frame, minute, *minutes)mins.pack(side=left)second = stringvar(root)seconds = ('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60')second.set(seconds[0])secs = optionmenu(frame, second, *seconds)secs.pack(side=left)button(root,text="set alarm",font=("helvetica 15"),command=threading).pack(pady=20)# 执行tkinter root.mainloop()
以上就是如何用python和tkinter制作一个简单的闹钟程序?的详细内容。
