徐州麦克技术服务有限公司崇左市地区分站 - 提供专业的解决方案!

崇左市

当前位置: 崇左市 > 政策法规 > 文章页

薪资计算工具

时间:2025-09-01 01:08来源:网络编辑:崇左市 点击: 10 次
忘记在哪里看到了,模仿着搞了一个一个简洁版,直接上运行截图。只需要在配置文件写上日薪,每日上班时间,每日下班时间即可

kangta520 发表于 2024-7-26 12:45
谢谢分享。提供源码吗?


import configparser
import time
from datetime import datetime, timedelta
import pystray
from PIL import Image, ImageDraw
import threading
import ctypes
import sys

def read_config(config_file):
    config = configparser.ConfigParser()
    config.read(config_file)
    daily_salary = float(config['work']['daily_salary'])
    start_time = config['work']['start_time']
    end_time = config['work']['end_time']
    return daily_salary, start_time, end_time

def calculate_earnings(daily_salary, start_time, end_time):
    now = datetime.now()
    today = now.date()
   
    start_time = datetime.strptime(start_time, "%H:%M").time()
    end_time = datetime.strptime(end_time, "%H:%M").time()
   
    start_datetime = datetime.combine(today, start_time)
    end_datetime = datetime.combine(today, end_time)
   
    if now < start_datetime:
        earned_amount = 0.00
        time_to_end = end_datetime - start_datetime
    elif now > end_datetime:
        worked_time = end_datetime - start_datetime
        time_to_end = timedelta(0)
    else:
        worked_time = now - start_datetime
        time_to_end = end_datetime - now
   
    if now >= start_datetime and now <= end_datetime:
        worked_hours = worked_time.total_seconds() / 3600
        hourly_rate = daily_salary / ((end_datetime - start_datetime).total_seconds() / 3600)
        earned_amount = worked_hours * hourly_rate
    else:
        earned_amount = 0.00

    hours, remainder = divmod(time_to_end.total_seconds(), 3600)
    minutes, seconds = divmod(remainder, 60)
   
    return round(earned_amount, 4), int(hours), int(minutes), int(seconds)

def create_image(width, height, color1, color2):
    image = Image.new('RGB', (width, height), color1)
    dc = ImageDraw.Draw(image)
    dc.rectangle(
        (width // 2, 0, width, height // 2),
        fill=color2)
    dc.rectangle(
        (0, height // 2, width // 2, height),
        fill=color2)
    return image

def update_tray_icon(icon, config_file):
    daily_salary, start_time, end_time = read_config(config_file)
    while True:
        earnings, hours, minutes, seconds = calculate_earnings(daily_salary, start_time, end_time)
        icon.title = f"已经赚取: &#165;{earnings:.4f}\n距离下班: {hours}小时{minutes}分{seconds}秒"
        time.sleep(1)
def on_quit(icon):
    icon.stop()
def about():
    ctypes.windll.user32.MessageBoxW(0, "该程序由Swback制作", "关于", 0)
def main():
    config_file = 'config.ini'
    icon_image = create_image(64, 64, 'black', 'white')
    icon = pystray.Icon("earnings", icon_image, "请确保config.ini存在")
    icon.menu = pystray.Menu(
        pystray.MenuItem("关于", about),
        pystray.MenuItem("退出", on_quit)
    )
    icon_thread = threading.Thread(target=update_tray_icon, args=(icon, config_file), daemon=True)
    icon_thread.start()
   
    icon.run()

if __name__ == "__main__":
    main()
 


(提示:崇左市麦克技术服务有限公司为您提供崇左市专业的劳务派遣公司、崇左市劳务外包公司、崇左市最好的劳务派遣公司、崇左市劳务服务外包公司、崇左市物业管理服务、崇左市保安服务、崇左市人事代理、崇左市人力资源外包服务公司、崇左市区域人力资源服务商)
------分隔线----------------------------