本帖最后由 william0712 于 2024-11-14 10:51 编辑
前段时间把媳妇的生日忘记了,差点跪键盘,自己记性不好,特意写了这个生日提醒器,输入完牛一的那人基本信息后,程序直接退出都没问题,到了生日那天早上9点整,你的电脑会自动弹出一个窗口提醒你“某某某今天生日”的字样,之后每年都会自动提醒,完全不用再次运行本程序,也会自动创建下一年的提醒任务。使用说明:解压出来的三个文件,放在同一目录下即可。
原理:一个自动化工具,用于在指定的日期提醒用户某个人的生日。通过创建 Windows 任务计划来实现这一功能,并且能够在每年的同一天自动创建新的任务计划,确保每年都能收到提醒。主要的技术栈包括 Python、批处理文件和 Windows 任务计划。每个部分协同工作,确保任务的创建和执行都能顺利进行。
PS:火绒查毒没问题,但360那破玩意可能会报毒,懂的都知道这垃圾玩意,在python连打包一句Print“ Hello world!"都报毒,自选或看代码自己打包也行,不说了这破360了,说了就来气。
运行截图如下:
运行截图汇总
觉得可以的麻烦点个赞,话不多说,上链接,蓝奏云:
https://wwrd.lanzouv.com/iNt8Q2dx2j4j 密码:8it7
哎,好像需求不大,毁灭吧,这个停更算了。
附代码:
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import messagebox, simpledialog
from lunardate import LunarDate
import os
import subprocess
from datetime import datetime, timedelta
def convert_to_lunar(gregorian_date):
"""Convert Gregorian date to Lunar date."""
lunar_date = LunarDate.fromSolarDate(gregorian_date.year, gregorian_date.month, gregorian_date.day)
return lunar_date
def create_bat_file(name, birth_date):
"""Create a batch file that displays a message box with the birthday reminder and creates the next year's task."""
bat_content = f"""@echo off
title 生日提醒器
echo {name}今天生日啦!
ping localhost -n 2 >nul
pause
:: 调用创建下一年任务的批处理文件
set TASK_NAME={name}
set BIRTH_YEAR={birth_date.year}
set BIRTH_MONTH={birth_date.month}
set BIRTH_DAY={birth_date.day}
set BAT_PATH=%~dp0reminder.bat
call "%~dp0create_next_year_task.bat" "%TASK_NAME%" "%BIRTH_YEAR%" "%BIRTH_MONTH%" "%BIRTH_DAY%" "%BAT_PATH%"
"""
bat_path = os.path.join(os.getcwd(), 'reminder.bat')
with open(bat_path, 'w', encoding='gbk') as bat_file:
bat_file.write(bat_content)
print(f"Created batch file at: {bat_path}")
def calculate_next_birthday(birth_date):
"""Calculate the next birthday from today's date."""
today = datetime.now().date()
next_birthday = datetime(today.year, birth_date.month, birth_date.day).date()
if next_birthday <= today:
next_birthday = datetime(today.year + 1, birth_date.month, birth_date.day).date()
return next_birthday
def create_scheduled_task(name, birth_date):
"""Create a scheduled task that runs the batch file on the specified date."""
# Calculate the next birthday
next_birthday = calculate_next_birthday(birth_date)
year = next_birthday.year
month = next_birthday.month
day = next_birthday.day
# Create task using .bat script
bat_script_path = os.path.join(os.getcwd(), 'create_task.bat')
bat_path = os.path.join(os.getcwd(), 'reminder.bat')
start_date = f"{year}/{month:02d}/{day:02d}" # 修改日期格式为 YYYY/MM/DD
start_time = '09:00'
# Check if the batch file exists
if not os.path.exists(bat_script_path):
messagebox.showerror("Error", f"Batch file not found at {bat_script_path}.")
return
if not os.path.exists(bat_path):
messagebox.showerror("Error", f"Reminder batch file not found at {bat_path}.")
return
command = [bat_script_path, name, bat_path, start_date, start_time]
try:
result = subprocess.run(command, capture_output=True, text=True, check=True)
print(f"Command output: {result.stdout}")
print(f"Command error: {result.stderr}")
messagebox.showinfo("Success", f"您的 '{name}' 自动提醒已成功创建,本程序可以关闭了.")
except subprocess.CalledProcessError as e:
messagebox.showerror("Error", f"Failed to create task '{name}': {e}\n{e.output}\n{e.stderr}")
except Exception as e:
messagebox.showerror("Error", f"Failed to create task '{name}': {e}")
def add_birthday():
"""Add a new birthday entry and create necessary files and tasks."""
name = simpledialog.askstring("Input", "请输入名字:")
if not name:
return
date_str = simpledialog.askstring("Input", "请输入公历生日日期(格式:YYYY-MM-DD):")
if not date_str:
return
try:
birth_date = datetime.strptime(date_str, '%Y-%m-%d').date()
except ValueError:
messagebox.showerror("Error", "无效的日期格式,请重新输入。")
return
create_bat_file(name, birth_date)
create_scheduled_task(name, birth_date)
def main():
root = tk.Tk()
root.title("生日提醒器 by_KOG丛林")
root.geometry("300x150")
label = tk.Label(root, text="欢迎使用生日提醒器!")
label.pack(pady=10)
button = tk.Button(root, text="添加生日", command=add_birthday)
button.pack(pady=10)
root.mainloop()
if __name__ == "__main__":
main()
|