Python实现闹钟功能

        目录结构如下:

        操作方式:

1、启动: py -3 clock.py
2、输入定时事件: input.bat

催化,上酸菜:

clock.py:

# -*-encoding:utf8-*-

import tkinter 
import tkinter.messagebox
import threading
import time
import os


TIMER_MAIN = None
FILE_NAME_CLOCK = "timer_clock_txt.txt"
TIMER_DIFF = 10

def BuildMessageBox(strNotice, strMessage):
    root = tkinter.Tk()
    root.wm_attributes('-topmost', 1)
    root.title(strNotice)
    tkinter.Label(root, text=strMessage, anchor="center", width=50, height=10).pack()
    root.mainloop()
    
    # 另外版本
    #root.withdraw()
    #tkinter.messagebox.showinfo(strNotice, strMessage)

def FuncTimerHandle(clock_desc):
    print("NOTICE: 提醒事件到期 %s" %clock_desc)
    BuildMessageBox("Notice", clock_desc)

def GetClockTimeAndDesc(line):
    str_list = line.split('#')
    if len(str_list) != 2 :
        print("Error: desc(%s) error" %line)
        return 0, ""
    return str_list[0], str_list[1]

def FuncFileHandle():
    print("DEBUG: ONCE FOR...")
    file_dir_input = os.getcwd() + "\\" + FILE_NAME_CLOCK
    f_input = open(file_dir_input, "r+")
    lines = f_input.readlines()
    for line in lines :
        clock_time, clock_desc = GetClockTimeAndDesc(line)
        if 0 == clock_time :
            continue
        now_time = time.time()
        diff_time = int(float(clock_time) - now_time)
        if diff_time <= 0 :
            print("Error: time is too small(%s, %d)" %(str(clock_time), now_time))
        timer = threading.Timer(diff_time, lambda: FuncTimerHandle(clock_desc))
        timer.start()
        print("DEBUG: 新增提醒事件 %s(%s) 提醒 %s" %(str(clock_time), str(diff_time), clock_desc))
    f_input.truncate(0) # 清空

    global TIMER_DIFF
    TIMER_MAIN = threading.Timer(TIMER_DIFF, FuncFileHandle)
    TIMER_MAIN.start()

def main():
    global TIMER_MAIN
    TIMER_MAIN = threading.Timer(1, FuncFileHandle)
    TIMER_MAIN.start()
    while(1) :
        input("")
    TIMER_MAIN.cancel()

main()

clock_input.py:

# -*-encoding:utf8-*-

import os
import time
import threading
import time

FILE_NAME_CLOCK = "timer_clock_txt.txt"

GLOBAL_EXIT_FLAG = 0

def TimeClockInput():
    while(1):
        time_time = ""
        time_stamp = 0
        while(1):
            time_time = input("请输入提醒时间(Example: 2021-6-24-16-20-0):")
            str_list = time_time.split('-')
            if len(str_list) == 6 :
                time_array = time.strptime(time_time, "%Y-%m-%d-%H-%M-%S")
                #print(time_array)
                time_stamp = time.mktime(time_array)
                time_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
                break
            print("Error: 格式错误!")
        time_desc = ""
        while(1):
            time_desc = input("请输入提醒文本:")
            break
        while(1):
            input_char = input("NOTICE: 即将定时,请输入(y/n)确认:")
            if input_char == 'y' or input_char == 'Y' :
                print("\nSUCCESS: 老妹儿,干得漂亮!成功定时在(%s)提醒您(%s)" %(str(time_time), time_desc))
                file_dir_input = os.getcwd() + "\\" + FILE_NAME_CLOCK
                f_input = open(file_dir_input, "w")
                f_input.write(str(time_stamp) + "#" + time_desc)
                f_input.close()
                global GLOBAL_EXIT_FLAG
                GLOBAL_EXIT_FLAG = 1
                return  
            elif input_char == 'n' or input_char == 'N' :
                print("ERROR: 哎呀妈呀,整这老大功夫,你现在说你输错了,害")
                break
            else :
                print("ERROR: 老妹儿啊,看准点,只让输(y/n)")

def TitleHandle(idx):
    next_time = 0
    if 0 == idx :
        print("=====================================================")
        next_time = 0.3
    elif 1 == idx :
        print("===== 来了,老妹儿!")
        next_time = 0.5
    elif 2 == idx :
        print("===== 来,定个时,搞个提醒不耽误事儿")
        next_time = 0.5
    elif 3 == idx :
        print("===== 分分钟几百万上下,咋能没个秘书呢")
        next_time = 0.8
    elif 4 == idx :
        print("===== 嘿,你还真没有")
        next_time = 1
    elif 5 == idx :
        print("===== 没事啊,老弟,你还有我,我给你记着,妥妥的,没点问题")
        next_time = 1
    elif 6 == idx :
        print("===== 你就放心造,可就造就完事儿了")
        next_time = 1
    elif 7 == idx :
        print("===== 随便造啊,到点我提醒你")
        next_time = 0.8
    else :
        print("======================================================")
        next_time = 0
    if 0 == next_time :
        TimeClockInput()
    else :
        TIMER_MAIN = threading.Timer(next_time, lambda: TitleHandle(idx+1))
        TIMER_MAIN.start()


TIMER_MAIN = threading.Timer(1, lambda: TitleHandle(0))
TIMER_MAIN.start()
while(0 == GLOBAL_EXIT_FLAG):
    continue

input.bat:

start py -3 clock_input.py

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值