|
import tkinter as tk
from datetime import datetime
# ------------------ 配置参数 ------------------
TARGET_DATE = datetime(2026, 12, 24, 0, 0, 0) # 考研日期(当天0点)
TITLE_TEXT = "27考研倒计时"
DATE_TEXT = "考试日期:2026年12月24日"
MOTTO_TEXT = "克服困难,勇敢者自有千方百计,怯懦者只感到万般无奈。"
# 颜色与字体
BG_COLOR = "#0a0f0f" # 深色背景
FG_TITLE = "#ffffff"
FG_COUNTDOWN = "#a3ff73" # 亮绿色
FG_DATE = "#cccccc"
FG_MOTTO = "#ffd966" # 柔和黄色
FONT_TITLE = ("微软雅黑", 20, "bold")
FONT_COUNTDOWN = ("Consolas", 18, "bold") # 等宽字体,保持宽度稳定
FONT_DATE = ("微软雅黑", 12)
FONT_MOTTO = ("楷体", 13, "italic")
# 窗口透明度 (0~1)
WINDOW_ALPHA = 0.92
# 右下角边距
MARGIN_RIGHT = 30
MARGIN_BOTTOM = 50
# ------------------ 功能函数 ------------------
def update_countdown():
"""更新时间显示"""
now = datetime.now()
delta = TARGET_DATE - now
if delta.total_seconds() < 0:
# 考试已过或已开始
countdown_label.config(text="???? 考研已开始,加油! ????")
return # 停止刷新
else:
days = delta.days
seconds = delta.seconds
hours = seconds // 3600
minutes = (seconds % 3600) // 60
secs = seconds % 60
# 格式:还剩 XXX天 HH:MM:SS
time_str = f"还剩 {days} 天 {hours:02d}:{minutes:02d}:{secs:02d}"
countdown_label.config(text=time_str)
# 每秒调用自己
root.after(1000, update_countdown)
def start_move(event):
"""记录拖动起点"""
root.drag_start_x = event.x
root.drag_start_y = event.y
def do_move(event):
"""窗口拖动"""
deltax = event.x - root.drag_start_x
deltay = event.y - root.drag_start_y
x = root.winfo_x() + deltax
y = root.winfo_y() + deltay
root.geometry(f"+{x}+{y}")
def quit_app(event):
"""右键退出程序"""
root.destroy()
# ------------------ 创建窗口 ------------------
root = tk.Tk()
root.title("考研倒计时") # 任务栏显示,但会被覆盖
root.overrideredirect(True) # 去掉窗口边框
root.attributes('-topmost', True) # 始终置顶
root.attributes('-alpha', WINDOW_ALPHA) # 透明度
root.configure(bg=BG_COLOR)
# ------------------ 放置控件 ------------------
# 使用Frame作为容器,方便统一背景和内边距
main_frame = tk.Frame(root, bg=BG_COLOR)
main_frame.pack(padx=15, pady=12)
# 标题
title_label = tk.Label(main_frame, text=TITLE_TEXT, font=FONT_TITLE,
fg=FG_TITLE, bg=BG_COLOR)
title_label.pack(pady=(0, 8))
# 倒计时(核心,会动态更新)
countdown_label = tk.Label(main_frame, text="正在计算...", font=FONT_COUNTDOWN,
fg=FG_COUNTDOWN, bg=BG_COLOR)
countdown_label.pack(pady=5)
# 考试日期
date_label = tk.Label(main_frame, text=DATE_TEXT, font=FONT_DATE,
fg=FG_DATE, bg=BG_COLOR)
date_label.pack(pady=5)
# 励志名言(自动换行,宽度约400像素)
motto_label = tk.Label(main_frame, text=MOTTO_TEXT, font=FONT_MOTTO,
fg=FG_MOTTO, bg=BG_COLOR, wraplength=400, justify="center")
motto_label.pack(pady=8)
# ------------------ 绑定事件 ------------------
root.bind('<Button-1>', start_move) # 左键按下记录位置
root.bind('<B1-Motion>', do_move) # 左键移动拖动窗口
root.bind('<Button-3>', quit_app) # 右键退出程序
# 为了让所有区域都能拖动和退出,给所有子控件也绑定相同事件(可选)
for widget in (title_label, countdown_label, date_label, motto_label):
widget.bind('<Button-1>', start_move)
widget.bind('<B1-Motion>', do_move)
widget.bind('<Button-3>', quit_app)
# ------------------ 窗口初始位置(右下角) ------------------
root.update_idletasks() # 更新控件布局,获取真实宽高
win_width = root.winfo_width()
win_height = root.winfo_height()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = screen_width - win_width - MARGIN_RIGHT
y = screen_height - win_height - MARGIN_BOTTOM
root.geometry(f'+{x}+{y}')
# ------------------ 启动倒计时刷新 ------------------
update_countdown()
# ------------------ 进入消息循环 ------------------
root.mainloop()
|