background
想着好玩,就写了个hvv工资实时统计的脚本,可以实时看到收入,可视化的感觉还是很不错的。于是就写了脚本,一开始仅仅是程序运行之后从0开始计数,每过一秒钟工资就增长一部分。后来我朋友说这逻辑不行。得自动算出累计的时间。我一听,好想法。于是乎就开始研究了。当然我全程啥也没做,全部交给GPT了,需要注意的就是这个工作时间,GPT错了很久才纠正过来。工作时间是不能按照24小时来计算的,并且还要考虑跨天的问题。
最终解决了问题,有炫酷的页面
from datetime import datetime, timedelta, time import tkinter as tk import random
def calculate_working_hours(start_datetime, end_datetime, work_start_time, work_end_time): total_duration = timedelta() current_datetime = start_datetime while current_datetime < end_datetime: work_start = current_datetime.replace(hour=work_start_time.hour, minute=work_start_time.minute) work_end = current_datetime.replace(hour=work_end_time.hour, minute=work_end_time.minute) if work_end < work_start: work_end += timedelta(days=1) if work_end > end_datetime: work_end = end_datetime work_duration = work_end - work_start total_duration += work_duration current_datetime += timedelta(days=1) return total_duration
def random_color(): return "#{:02x}{:02x}{:02x}".format(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
def update_money_label(): total_working_duration = calculate_working_hours(start_datetime, datetime.now(), work_start_time, work_end_time) total_seconds = total_working_duration.total_seconds() money = total_seconds * 0.0277777777777 money_label.config(text=f"你已经挣了:{money:.2f} 元") current_time = datetime.now().time() if work_start_time <= current_time < work_end_time: money_label.config(fg=random_color()) else: money_label.config(fg=random_color()) money_label.after(1000, update_money_label)
root = tk.Tk() root.title("Hvv收入实时展示")
start_datetime = datetime(2023, 8, 9, 20, 30) work_start_time = time(20, 30) work_end_time = time(8, 30)
money_label = tk.Label(root, text="工资:0.00 元", font=("Helvetica", 24)) money_label.pack(pady=20)
update_money_label()
root.mainloop()
|
封装exe
安装库
pip install pyinstaller pyinstaller your_script_name.py
|
这样生成的有黑窗口不好看,隐藏掉
pyinstaller --noconsole your_script_name.py
|
是不是把数字调高点,我挣钱的速度也就快了?哈哈哈哈