目录
先决条件
对python和GUI有良好的认知:Python与Tkinter结合使用,提供了一种快速简便的方法来创建GUI应用程序。Tkinter为Tk GUI工具包提供了一个强大的面向对象接口。所使用的所有模块都不需要像其他库(如NumPy)那样事先下载,因此该项目将是用户友好的,并且可以在任何用于python编程的虚拟环境中访问
1 导入必要的库和模块
代码如下:
#Importing all the necessary libraries to form the alarm clock:
from tkinter import *
import datetime
import time
import winsound
代码解析:
- Tkinter模块属于Python的GUI标准库。它帮助我们创建一个对话框,其中包含我们想要提供或从用户那里获得的任何信息;
datetime
和time
是两个常用的模块,分别用于处理日期和时间相关的任务;- Winsound模块提供了对Windows平台提供的基本声音播放机制的访问。这对于在调用函数时立即生成声音很有用。
2 使用tkinter创建GUI对话框
2.1 创建主窗口
clock = Tk()
创建了一个Tk
实例clock,这是tkinter
的一个顶级窗口,所有的组件都将放置在这个窗口内
clock.title("Python Alarm Clock")
设置窗口的标题为"Python Alarm Clock"
clock.iconbitmap(r"clock_120705.ico")
设置窗口的图标为指定的.ico
文件。这里使用了r
前缀来表示原始字符串,避免转义序列的问题。
注意:.ico
文件需要在打开的power shell终端路径中,或使用绝对路径,绝对路径参考如下:
clock.iconbitmap(r"G:\learning\python\projects\clock\DataFlair-Alarm-Clock\clock_120705.ico")
clock.geometry("400x200")
设置窗口的初始大小为400像素宽,200像素高
2.2 创建标签(Label)
Label
有许多属性可以用来定制它的外观和行为。以下是一些常用的属性:
text
: 设置标签上显示的文本。bg
,background
: 设置背景颜色。fg
,foreground
: 设置文本的颜色。font
: 设置字体样式,例如('Times', 18, 'bold italic')
。width
,height
: 设置标签的宽度和高度。anchor
: 文本在标签中的对齐方式,例如'nw'
表示左上角对齐。relief
: 设置边框样式,如'raised'
、'sunken'
等。image
: 设置显示的图片(需要一个Tkinter图像对象)。compound
: 当设置text
和image
时,决定它们的排列方式。
time_format=Label(clock, text= "Enter time in 24 hour format!", fg="red",bg="black",font="Arial").place(x=60,y=120)
创建一个标签,显示文本“Enter time in 24 hour format!”,字体颜色为红色,背景为黑色,字体为Arial,并放置在坐标(60, 120)处;
addTime = Label(clock,text = "Hour Min Sec",font=60).place(x = 110)
创建另一个标签,显示文本“Hour Min Sec”,字体大小为60,并放置在坐标(110, 默认y值)处;
setYourAlarm = Label(clock,text = "When to wake you up",fg="blue",relief = "solid",font=("Helevetica",7,"bold")).place(x=0, y=29)
创建第三个标签,显示文本“When to wake you up”,字体颜色为蓝色,边框样式为实线,字体为Helvetica,字号为7,粗体,并放置在坐标(0, 29)处。
2.3 创建输入框
# The Variables we require to set the alarm(initialization):
hour = StringVar()
min = StringVar()
sec = StringVar()
创建三个StringVar
变量,用于存储用户输入的小时、分钟和秒数。
#Time required to set the alarm clock:
hourTime= Entry(clock,textvariable = hour,bg = "pink",width = 15).place(x=120,y=30)
minTime= Entry(clock,textvariable = min,bg = "pink",width = 15).place(x=160,y=30)
secTime = Entry(clock,textvariable = sec,bg = "pink",width = 15).place(x=210,y=30)
创建三个输入框,分别绑定到上面定义的StringVar
变量,并设置背景颜色为粉色,宽度为15个字符,并放置在指定的位置。
2.4 创建按钮
#To take the time input by user:
submit = Button(clock,text = "Set Alarm",fg="red",width = 10,command = actual_time).place(x =110,y=70)
创建一个按钮,显示文本“Set Alarm”,字体颜色为红色,宽度为10个字符,点击按钮时调用actual_time()
函数,并放置在坐标(110, 70)处。
3 按钮处理
3.1 获取输入的时间
def actual_time():
set_alarm_timer = f"{hour.get()}:{min.get()}:{sec.get()}"
alarm(set_alarm_timer)
- 定义了一个名为
actual_time
的函数, - 使用格式化字符串(f-string)来构建一个时间字符串
set_alarm_timer
。它从hour
、min
和sec
这三个StringVar
对象中获取用户的输入值,并将它们以HH:MM:SS
的格式拼接在一起; - 调用
alarm
函数,将set_alarm_timer
作为函数的参数;
3.2 响铃处理
def alarm(set_alarm_timer):
while True:
time.sleep(1)
current_time = datetime.datetime.now()
now = current_time.strftime("%H:%M:%S")
date = current_time.strftime("%d/%m/%Y")
print("The Set Date is:",date)
print(now)
if now == set_alarm_timer:
print("Time to Wake up")
winsound.PlaySound("skype提示音.wav",winsound.SND_ASYNC)
break
- 启动无限循环
- 每隔1s获取当前时间,将时间格式化为
HH:MM:SS
(小时:分钟:秒)使当前时间字符串与set_alarm_timer
字符串形式一致,以便进行比较。 - 在终端中打印当前时间字符串
- 当前时间
now
是否与用户设定的时间set_alarm_timer
相匹配,如果匹配,打印消息“Time to Wake up”,使用winsound.PlaySound函数播放"skype提示音.wav"的音频文件;
4 运行代码
在终端中运行代码,输出对话框如下:
输入闹钟时间,到达时间点后电脑播放"skype提示音.wav"音频文件;
5 完整代码
#Importing all the necessary libraries to form the alarm clock:
from tkinter import *
import datetime
import time
import winsound
def alarm(set_alarm_timer):
while True:
time.sleep(1)
current_time = datetime.datetime.now()
now = current_time.strftime("%H:%M:%S")
date = current_time.strftime("%d/%m/%Y")
print("The Set Date is:",date)
print(now)
if now == set_alarm_timer:
print("Time to Wake up")
winsound.PlaySound("skype提示音.wav",winsound.SND_ASYNC)
break
def actual_time():
set_alarm_timer = f"{hour.get()}:{min.get()}:{sec.get()}"
alarm(set_alarm_timer)
clock = Tk()
clock.title("Python Alarm Clock")
clock.iconbitmap(r"clock_120705.ico")
clock.geometry("400x200")
time_format=Label(clock, text= "Enter time in 24 hour format!", fg="red",bg="black",font="Arial").place(x=60,y=120)
addTime = Label(clock,text = "Hour Min Sec",font=60).place(x = 110)
setYourAlarm = Label(clock,text = "When to wake you up",fg="blue",relief = "solid",font=("Helevetica",7,"bold")).place(x=0, y=29)
# The Variables we require to set the alarm(initialization):
hour = StringVar()
min = StringVar()
sec = StringVar()
#Time required to set the alarm clock:
hourTime= Entry(clock,textvariable = hour,bg = "pink",width = 15).place(x=120,y=30)
minTime= Entry(clock,textvariable = min,bg = "pink",width = 15).place(x=160,y=30)
secTime = Entry(clock,textvariable = sec,bg = "pink",width = 15).place(x=210,y=30)
#To take the time input by user:
submit = Button(clock,text = "Set Alarm",fg="red",width = 10,command = actual_time).place(x =110,y=70)
clock.mainloop()
#Execution of the window.
5 源码下载
源码、音频文件、icon图标下载地址:https://download.csdn.net/download/sb2220/89669949https://download.csdn.net/download/sb2220/89669949