功能描述:
开发环境:
涉及知识点:
代码实现
import os
import tkinter
import time
from threading import Thread
from datetime import datetime
import tkinter.messagebox as ms
import ctypes
player = ctypes.windll.kernel32
root = tkinter.Tk()
root.title("我的时钟")
root.minsize(width=300, height=400)
now_time_hour = datetime.now().hour
now_time_min = datetime.now().minute
now_time_sec = datetime.now().second
hour = tkinter.StringVar(value=now_time_hour)
minute = tkinter.StringVar(value=now_time_min)
second = tkinter.StringVar(value=now_time_sec)
clock_hour = 0
clock_minute = 0
clock_second = 0
clock_flag = False
def clock_set():
global clock_hour, clock_minute, clock_second
clock_hour = int(hour_entry.get())
clock_minute = int(minute_entry.get())
clock_second = int(second_entry.get())
ms.showwarning("重置闹钟", "闹钟已重置!\n")
def start_set():
global clock_flag
clock_flag = not clock_flag
if clock_flag:
ms.showwarning("闹钟开启", "闹钟成功开启!\n{}时{}分{}秒响铃".format(clock_hour, clock_minute, clock_second))
def timer():
while True:
second_num = int(second.get())
minute_num = int(minute.get())
hour_num = int(hour.get())
if clock_flag:
confirm_bt["text"] = "ON"
else:
confirm_bt["text"] = "OFF"
if clock_flag and (hour_num, minute_num, second_num) == (clock_hour, clock_minute, clock_second):
for i in range(3):
player.Beep(2000, 1000)
# 这里可以添加其他定时任务,关机,启动桌面应用....
print("叮 叮 叮 . . .")
os.system("shutdown -s -t 600")
second_num += 1
if second_num == 60:
second_num = 0
minute_num += 1
if minute_num == 60:
minute_num = 0
hour_num += 1
if hour_num == 24:
hour_num = 0
hour.set("%02d" % hour_num)
minute.set("%02d" % minute_num)
second.set("%02d" % second_num)
time.sleep(1)
if __name__ == '__main__':
current_time_lab = tkinter.Label(root, width=10, height=1, text="现在时间:", font=3)
current_time_lab.place(x=10, y=5)
hour_lab = tkinter.Label(root, width=3, height=1, textvariable=hour, bg="#D6EAF8", font=1)
minute_lab = tkinter.Label(root, width=3, height=1, textvariable=minute, bg="#D6EAF8", font=1)
second_lab = tkinter.Label(root, width=3, height=1, textvariable=second, bg="#D6EAF8", font=1)
sep_lab1 = tkinter.Label(root, width=1, height=1, text=":", font=1)
sep_lab2 = tkinter.Label(root, width=1, height=1, text=":", font=1)
hour_lab.place(x=40, y=40)
sep_lab1.place(x=75, y=40)
minute_lab.place(x=90, y=40)
sep_lab2.place(x=125, y=40)
second_lab.place(x=140, y=40)
clock_set_lab = tkinter.Label(root, width=10, height=1, text="闹钟设置:", font=3)
clock_set_lab.place(x=10, y=80)
hour_entry = tkinter.Entry(root, width=3, bg="#00CC00", justify="center", font=1)
minute_entry = tkinter.Entry(root, width=3, bg="#00CC00", justify="center", font=1)
second_entry = tkinter.Entry(root, width=3, bg="#00CC00", justify="center", font=1)
sep_lab3 = tkinter.Label(root, width=1, height=1, text=":", font=1)
sep_lab14 = tkinter.Label(root, width=1, height=1, text=":", font=1)
hour_entry.place(x=40, y=120)
sep_lab3.place(x=75, y=120)
minute_entry.place(x=90, y=120)
sep_lab14.place(x=125, y=120)
second_entry.place(x=140, y=120)
hour_entry.insert("end", "00")
minute_entry.insert("end", "00")
second_entry.insert("end", "00")
set_bt = tkinter.Button(root, text="重置闹钟", width=10, height=1, bg="#F5CBA7", command=clock_set)
confirm_bt = tkinter.Button(root, text="OFF", width=10, height=1, bg="#D35400", command=start_set)
set_bt.place(x=35, y=160)
confirm_bt.place(x=135, y=160)
clock_t = Thread(target=timer)
clock_t.start()
root.mainloop()