使用 Tkinter 创建闹钟
先决条件: Python GUI – tkinter、winsound、时间和日期时间。
众所周知,现在按时起床是一项非常艰巨的任务。解决方案是闹钟。在本文中,我们将学习如何在Python中使用 Tkinter 创建闹钟。如果没有警报,几个人会睡过头,上班迟到。闹钟也可能有助于保持规律的睡眠时间表。
- Tkinter: Python为开发 GUI(图形用户界面)提供了多种选择。在所有 GUI 策略中,tkinter 是最常用的技术。它是Python附带的 Tk GUI 工具包的惯用Python接口。
- Winsound: winsound 模块提供对 Windows 平台提供的基本声音播放机制的访问。它包括函数和许多其他常量。使 PC 的扬声器发出哔哔声。
- 时间: Python中的时间模块提供了各种与时间相关的函数。这个模块带有 Python 的普通模块。
- datetime:datetime的主要重点是使访问与日期、时间和时区相关联的事物的属性变得更简单。
下面是 GUI 的样子:-
让我们一步一步地理解实现:
步骤 1:导入所需的库
Python3
# Import Required Library
from tkinter import *
import datetime
import time
import winsound
Python3
def alarm():
# Infintite Loop
while True:
# Set Alarm
set_alarm = f"{hour.get()}:{minute.get()}:{second.get()}"
# Wait for one seconds
time.sleep(1)
# Get current time
current_time = datetime.datetime.now().strftime("%H:%M:%S")
# Check whether set alarm is equal to current time or not
if current_time == set_alarm:
print("Time to Wake up")
# Playing sound
winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
Python3
# Import Required Library
from tkinter import *
import datetime
import time
import winsound
from threading import *
# Create Object
root = Tk()
# Set geometry
root.geometry("400x200")
# Use Threading
def Threading():
t1=Thread(target=alarm)
t1.start()
def alarm():
# Infintite Loop
while True:
# Set Alarm
set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
# Wait for one seconds
time.sleep(1)
# Get current time
current_time = datetime.datetime.now().strftime("%H:%M:%S")
print(current_time,set_alarm_time)
# Check whether set alarm is equal to current time or not
if current_time == set_alarm_time:
print("Time to Wake up")
# Playing sound
winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
# Add Labels, Frame, Button, Optionmenus
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)
# Execute Tkinter
root.mainloop()
步骤 2:添加按钮、标签、框架和选项菜单
句法:
# Button
Button(Object Name, text=”Enter Text”,**attr)
# Label
Label(Object Name, text=”Enter Text”, command=”Enter Command” , **attr)
# Frame
Frame(Object Name, **attr)
# Option Menu
OptionMenu(“Object Name”, “Data Type”, “list of value in form of tuple”, **attr)
我们将创建一个三选项菜单:-
- 小时 (00–24)
- 分钟 (00–60)
- 秒 (00–60)
时间采用 24 小时制时间格式。
第 3 步:创建一个名为alarm()的函数,该函数执行闹钟工作。
蟒蛇3
def alarm():
# Infintite Loop
while True:
# Set Alarm
set_alarm = f"{hour.get()}:{minute.get()}:{second.get()}"
# Wait for one seconds
time.sleep(1)
# Get current time
current_time = datetime.datetime.now().strftime("%H:%M:%S")
# Check whether set alarm is equal to current time or not
if current_time == set_alarm:
print("Time to Wake up")
# Playing sound
winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
下面是完整的实现:
- 无限循环
- 从用户处获取小时、分钟、秒值
- 使用时间模块等待一秒钟
- 使用datetime模块获取当前时间
- 检查当前时间是否等于设置时间;使用winsound模块播放声音
蟒蛇3
# Import Required Library
from tkinter import *
import datetime
import time
import winsound
from threading import *
# Create Object
root = Tk()
# Set geometry
root.geometry("400x200")
# Use Threading
def Threading():
t1=Thread(target=alarm)
t1.start()
def alarm():
# Infintite Loop
while True:
# Set Alarm
set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
# Wait for one seconds
time.sleep(1)
# Get current time
current_time = datetime.datetime.now().strftime("%H:%M:%S")
print(current_time,set_alarm_time)
# Check whether set alarm is equal to current time or not
if current_time == set_alarm_time:
print("Time to Wake up")
# Playing sound
winsound.PlaySound("sound.wav",winsound.SND_ASYNC)
# Add Labels, Frame, Button, Optionmenus
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)
# Execute Tkinter
root.mainloop()
输出: