功能描述:
當前時間展示
定時鬧鐘實現
可通過os模組拓展其他定時任務
開發環境:
涉及知識點:
gui介面tkinter模組的使用
函式定義與使用
基本語法結構的使用
字串,列表,元組的使用
多執行緒使用
時間模組的使用
型別轉換
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.strin**ar(value=now_time_hour)
minute = tkinter.strin**ar(value=now_time_min)
second = tkinter.strin**ar(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.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()
linux定時任務 at定時任務
at命令是一次性定時計畫任務,at的守護程序atd會以後臺模式執行,檢查作業佇列來執行作業。atd守護程序會檢查系統上的乙個特殊目錄來獲取at命令的提交的作業,預設情況下,atd守護程序每60秒檢查一次目錄,有作業時,會檢查作業執行時間,如果時間與當前時間匹配,則執行此作業。注意 at命令是一次性定...
定時任務 定時任務弊端與優化方案
使用定時任務關閉超期未支付訂單,會存在的弊端 1.會有時間差,程式不嚴謹 10 39下單,11 00檢查不足1小時,12 00檢查,超過1小時多餘39分鐘 2.不支援集群 單機沒毛病,使用集群後,就會有多個定時任務 解決方案 只使用一台計算機節點,單獨用來執行所有的定時任務 3.會對資料庫全表搜尋,...
延時任務與定時任務
1.延時任務 at命令的使用 1 開啟終端,在根目錄下建立乙個新的目錄取名為 at 2 進入 at目錄下,執行watch n 1 ls l at命令,對其進行監控 3 同時開啟另乙個終端,執行命令 at 某個將來的具體時刻 表示將來在某個時刻執行你特定的操作 例如下圖便是在17 20在 at目錄下建...