基于Python,便利店商品保质期计算

基于Python,对商品保质期计算​。

生产日期输入框会默认载入当前的日期,当生产日期不符请自行更改,保质期分别以时间单位“天”与“月”份进行计算​。

当点击“检查是否过期”按钮,会监测商品名称输入框,2个以下字符时,只会计算是否过期:2个字符以上,则将本次计算写入日志​。

import tkinter as tk
from tkinter import ttk
from datetime import datetime, timedelta
import os

# 设置窗口的透明度
def set_transparency(window, transparency):
    window.attributes("-alpha", transparency)

# 切换单位的函数
def toggle_unit(unit):
    global current_unit
    current_unit = unit
    update_input_label()

# 更新输入框和标签的函数
def update_input_label():
    if current_unit == "days":
        shelf_life_label.config(text="保质期(天):")
        shelf_life_entry.delete(0, tk.END)
        shelf_life_entry.insert(0, shelf_life_days)
    else:
        shelf_life_label.config(text="保质期(月):")
        shelf_life_entry.delete(0, tk.END)
        shelf_life_entry.insert(0, shelf_life_months)

# 计算是否过期的函数
def check_expiry():
    product_name = product_name_entry.get()
    if len(product_name) > 2:
        # 商品名称大于2个字符,写入日志
        production_date_str = production_date_entry.get()
        shelf_life_str = shelf_life_entry.get()
        expiry_status = result_label.cget("text")

        with open("D:\\daku\\保质期计算\\lo.txt", "a") as log_file:
            log_file.write(f"{datetime.now()} - {product_name} - 生产日期: {production_date_str} - 保质期: {shelf_life_str} {current_unit} - {expiry_status}\n")

    production_date = datetime.strptime(production_date_entry.get(), "%Y-%m-%d")
    if current_unit == "days":
        shelf_life = int(shelf_life_entry.get())
        expiry_date = production_date + timedelta(days=shelf_life)
    else:
        shelf_life = int(shelf_life_entry.get())
        # 将月份转换为天数,假设每月30天
        expiry_date = production_date + timedelta(days=shelf_life * 30)

    # 获取当前日期
    today = datetime.now()

    # 计算过期时间差
    time_diff = expiry_date - today

    # 检查是否过期
    if expiry_date < today:
        # 已过期,显示过期多久
        result_label.config(text=f"太糟糕啦,已过期 {time_diff.days} 天 嘤嘤嘤:(╥╯^╰╥)")
    else:
        # 未过期,显示多久过期
        result_label.config(text=f"未过期,剩余 {time_diff.days} 天过期,ᄽ●・●ᄿ")

# 创建主窗口
root = tk.Tk()
root.geometry("470x250")
set_transparency(root, 0.9)  # 设置透明度为90%

# 获取当前日期
current_date = datetime.now().strftime("%Y-%m-%d")

# 新增商品名称输入框和标签
product_name_label = ttk.Label(root, text="商品名称:")
product_name_label.pack()
product_name_entry = ttk.Entry(root)
product_name_entry.pack()

# 创建生产日期输入框和标签
production_date_label = ttk.Label(root, text="生产日期:")
production_date_label.pack()
production_date_entry = ttk.Entry(root)
production_date_entry.insert(0, current_date)  # 在输入框中插入当前日期
production_date_entry.pack()

# 初始化保质期为0
shelf_life_days = 0
shelf_life_months = 0
current_unit = "days"

# 创建保质期输入框和标签
shelf_life_label = ttk.Label(root, text="保质期(天):")
shelf_life_label.pack()
shelf_life_entry = ttk.Entry(root)
shelf_life_entry.insert(0, shelf_life_days)
shelf_life_entry.pack()

# 创建单位切换按钮
toggle_days_button = ttk.Button(root, text="天", command=lambda: toggle_unit("days"))
toggle_days_button.pack()

toggle_months_button = ttk.Button(root, text="月", command=lambda: toggle_unit("months"))
toggle_months_button.pack()

# 创建检查按钮
check_button = ttk.Button(root, text="检查是否过期", command=check_expiry)
check_button.pack()

# 显示结果的标签
result_label = ttk.Label(root, text="")
result_label.pack()

# 运行主循环
root.mainloop()

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值