QQ截图20241016095626.jpg (21.04 KB, 下载次数: 0)
下载附件
2024-10-16 09:56 上传
123云盘的下载链接:https://www.123684.com/s/EG5A-9R0AH
点开之后点那个浏览器下载就可以直接下载源文件了。
附代码:
[Python] 纯文本查看 复制代码import tkinter as tk
from tkinter import messagebox
import time
import threading
import sys
class TimerApp:
def __init__(self, master):
self.master = master
master.title("倒计时计时器 BY.KOG丛林")
# 设置窗口初始大小
master.geometry("400x340")
self.label = tk.Label(master, text="00:00:00", font=("Helvetica", 24))
self.label.pack(pady=10)
self.remaining_time = tk.StringVar()
self.remaining_time_label = tk.Label(master, textvariable=self.remaining_time)
self.remaining_time_label.pack(pady=5)
# 创建输入框和标签
input_frame = tk.Frame(master)
input_frame.pack(pady=10)
self.hour_entry = tk.Entry(input_frame, width=5, font=("Helvetica", 12))
self.hour_entry.grid(row=0, column=0, padx=(10, 5))
self.hour_entry.insert(0, "00")
hour_label = tk.Label(input_frame, text="小时", font=("Helvetica", 12))
hour_label.grid(row=0, column=1, padx=(0, 5))
self.minute_entry = tk.Entry(input_frame, width=5, font=("Helvetica", 12))
self.minute_entry.grid(row=0, column=2, padx=(0, 5))
self.minute_entry.insert(0, "00")
minute_label = tk.Label(input_frame, text="分钟", font=("Helvetica", 12))
minute_label.grid(row=0, column=3, padx=(0, 5))
self.second_entry = tk.Entry(input_frame, width=5, font=("Helvetica", 12))
self.second_entry.grid(row=0, column=4, padx=(0, 5))
self.second_entry.insert(0, "00")
second_label = tk.Label(input_frame, text="秒", font=("Helvetica", 12))
second_label.grid(row=0, column=5, padx=(0, 5))
# 自定义字体大小输入框
font_size_frame = tk.Frame(master)
font_size_frame.pack(pady=10)
self.font_size_entry = tk.Entry(font_size_frame, width=5, font=("Helvetica", 12))
self.font_size_entry.pack(side=tk.LEFT, padx=(5, 5))
self.font_size_entry.insert(0, "24") # 默认字体大小为 24
font_size_label = tk.Label(font_size_frame, text="字体大小", font=("Helvetica", 12))
font_size_label.pack(side=tk.LEFT, padx=(5, 5))
self.set_font_size_button = tk.Button(font_size_frame, text="确定", command=self.set_font_size)
self.set_font_size_button.pack(side=tk.LEFT, padx=(5, 5))
# 提醒时间输入框
reminder_frame = tk.Frame(master)
reminder_frame.pack(pady=10)
self.reminder_entry = tk.Entry(reminder_frame, width=5, font=("Helvetica", 12))
self.reminder_entry.pack(side=tk.LEFT, padx=(5, 5))
self.reminder_entry.insert(0, "5") # 默认提醒时间为 5 分钟
reminder_label = tk.Label(reminder_frame, text="剩余时间提醒", font=("Helvetica", 12))
reminder_label.pack(side=tk.LEFT, padx=(5, 5))
self.set_reminder_button = tk.Button(reminder_frame, text="确定", command=self.set_reminder_time)
self.set_reminder_button.pack(side=tk.LEFT, padx=(5, 5))
# 添加一个复选框来控制窗口是否置顶
topmost_frame = tk.Frame(master)
topmost_frame.pack(pady=10)
self.topmost_var = tk.IntVar()
self.topmost_checkbox = tk.Checkbutton(topmost_frame, text="窗口置顶", variable=self.topmost_var,
command=self.toggle_topmost)
self.topmost_checkbox.pack(side=tk.LEFT, padx=(5, 5))
# 创建按钮 Frame 并放置在最底部
button_frame = tk.Frame(master)
button_frame.pack(side=tk.BOTTOM, pady=10)
self.start_button = tk.Button(button_frame, text="开始", command=self.start_timer)
self.start_button.pack(side=tk.LEFT, padx=(5, 5))
self.stop_button = tk.Button(button_frame, text="停止", command=self.stop_timer)
self.stop_button.pack(side=tk.LEFT, padx=(5, 5))
self.is_running = False
self.seconds_left = 0
self.reminder_thread = None
self.reminder_minutes = int(self.reminder_entry.get())
self.shutdown_flag = False # 标志位,用于指示是否需要退出线程
# 初始化时设置窗口是否置顶
self.toggle_topmost()
# 绑定窗口关闭事件
self.master.protocol("WM_DELETE_WINDOW", self.on_closing)
def on_closing(self):
"""窗口关闭时的回调函数"""
self.shutdown_flag = True # 设置标志位
self.stop_timer() # 停止计时器
self.master.destroy() # 销毁主窗口
sys.exit() # 结束进程
def toggle_topmost(self):
if self.topmost_var.get() == 1:
self.master.attributes('-topmost', True)
else:
self.master.attributes('-topmost', False)
def start_timer(self):
try:
hours = int(self.hour_entry.get())
minutes = int(self.minute_entry.get())
seconds = int(self.second_entry.get())
self.seconds_left = hours * 3600 + minutes * 60 + seconds
self.is_running = True
self.timer_thread = threading.Thread(target=self.update_clock)
self.timer_thread.start()
if not self.reminder_thread or not self.reminder_thread.is_alive():
self.reminder_thread = threading.Thread(target=self.set_reminder)
self.reminder_thread.start()
except ValueError:
messagebox.showerror("输入错误", "请输入有效的数字")
def update_clock(self):
while self.seconds_left > 0 and self.is_running and not self.shutdown_flag:
mins, secs = divmod(self.seconds_left, 60)
hours, mins = divmod(mins, 60)
timeformat = f'{hours:02d}:{mins:02d}:{secs:02d}'
self.label.config(text=timeformat, font=("Helvetica", int(self.font_size_entry.get())))
self.seconds_left -= 1
time.sleep(1)
if self.seconds_left == 0:
self.is_running = False
messagebox.showinfo("计时结束", "计时已经结束!")
def stop_timer(self):
self.is_running = False
def set_reminder(self):
while self.seconds_left > 0 and self.is_running and not self.shutdown_flag:
remaining_mins = self.seconds_left // 60
if remaining_mins