首先通过大模型直接写一个计时程序
计时逻辑可以自己编造
import tkinter as tk
import time
class TimerApp:
def __init__(self, master):
self.master = master
master.title("简单计时器")
self.start_time = None
self.elapsed_time = 0
# 创建标签显示时间
self.time_label = tk.Label(master, text="00:00:00")
self.time_label.pack()
# 创建开始计时按钮
self.start_button = tk.Button(master, text="开始计时", command=self.start_timer)
self.start_button.pack()
# 创建停止计时按钮
self.stop_button = tk.Button(master, text="停止计时", command=self.stop_timer, state='disabled')
self.stop_button.pack()
# 创建清零按钮
self.reset_button = tk.Button(master, text="清零", command=self.reset_timer)
self.reset_button.pack()
# 更新时间的定时器
self.update_time = None
def start_timer(self):
if self.start_time is None:
self.start_time = time.time() - self.elapsed_time
self.update_timer()
self.start_button.config(state='disabled')
self.stop_button.config(state='normal')
def stop_timer(self):
if self.update_time is not None:
self.master.after_cancel(self.update_time)
self.update_time = None
self.elapsed_time = time.time() - self.start_time
self.start_button.config(state='normal')
self.stop_button.config(state='disabled')
def reset_timer(self):
self.elapsed_time = 0
self.time_label.config(text="00:00:00")
self.start_time = None # 重置start_time为None
if self.update_time is not None:
self.master.after_cancel(self.update_time)
self.update_time = None
self.start_button.config(state='normal')
self.stop_button.config(state='disabled')
def update_timer(self):
"""更新显示的时间"""
if self.start_time is not None:
current_time = time.time() - self.start_time
hours, remainder = divmod(int(current_time), 3600)
minutes, seconds = divmod(remainder, 60)
time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
self.time_label.config(text=time_str)
self.update_time = self.master.after(1000, self.update_timer)
root = tk.Tk()
app = TimerApp(root)
root.mainloop()
然后在环境里安装PyInstaller
pip install pyinstaller
最后在py文件目录下直接用命令行打包程序
Pyinstaller --onefile xxx.py
运行完成后,在dist目录下的xxx.exe即可直接使用 作者:技术叆 https://www.bilibili.com/read/cv34699164/?spm_id_from=333.999.0.0 出处:bilibili