基于Tkinter的工时计算程序:实现HH:MM格式转浮点数需求
解决HH:MM格式工时转浮点数并计算加班的问题
下面是修改后的完整代码,实现了HH:MM格式输入转浮点数的功能,同时保留原有的列表管理、加班计算逻辑,还加了输入格式验证避免程序崩溃:
from tkinter import * from tkinter import messagebox # Tkinter窗口初始化 win = Tk() win.geometry("500x500") win.title("Work Hours Calculator") win.resizable(False, False) my_list = [] # 存储原始输入的HH:MM字符串,方便对应显示 original_times = [] # 辅助函数:将HH:MM格式字符串转换为浮点小时数 def hhmm_to_float(time_str): try: hours, minutes = time_str.split(':') hours = int(hours) minutes = int(minutes) # 计算总小时数,保留两位小数匹配示例格式 total_hours = hours + minutes / 60 return round(total_hours, 2) except (ValueError, IndexError): # 格式错误时返回None return None # 添加工时输入 def inp(): input_str = actinp.get().strip() if not input_str: messagebox.showwarning("提示", "请输入HH:MM格式的工时") return float_hours = hhmm_to_float(input_str) if float_hours is None: messagebox.showerror("错误", "格式无效!请使用HH:MM格式(例如11:30)") return my_list.append(float_hours) original_times.append(input_str) # 更新显示:对应展示原始输入和转换后的小时数 display_text = "\n".join([f"{orig} → {hours} 小时" for orig, hours in zip(original_times, my_list)]) showlabel.config(text=display_text) # 清空输入框方便连续输入 actinp.delete(0, END) # 计算加班薪资 def show(): if not my_list: messagebox.showwarning("提示", "还未添加任何工时!") return try: rate = int(rateinp.get()) except ValueError: messagebox.showerror("错误", "请输入有效的时薪数字") return total_hours = sum(my_list) if total_hours <= 40: showlabel.config(text=f"总工时:{round(total_hours, 2)}小时\n无加班") else: overtime_hours = total_hours - 40 overtime_pay = overtime_hours * rate * 0.5 showlabel.config(text=f"总工时:{round(total_hours, 2)}小时\n加班时长:{round(overtime_hours, 2)}小时\n加班薪资:${round(overtime_pay, 2)}") # 清空所有记录 def clear(): my_list.clear() original_times.clear() showlabel.config(text=" ") # 删除最后一条记录 def pop(): if not my_list: messagebox.showwarning("提示", "没有记录可以删除!") return my_list.pop() original_times.pop() display_text = "\n".join([f"{orig} → {hours} 小时" for orig, hours in zip(original_times, my_list)]) showlabel.config(text=display_text) # 界面组件布局 kk = Label(win, width=70, height=1) kk.grid() showlabel = Label(win, width=71, height=10, bg='white', justify=LEFT, padx=5, pady=5) showlabel.grid() actlab = Label(win, text="输入工时(HH:MM)") # 更新提示文本 actlab.grid() actinp = Entry(win, width=10) # 加宽输入框适配HH:MM格式 actinp.grid() ratelab = Label(win, text="时薪($/小时)") ratelab.grid() rateinp = Entry(win, width=5) rateinp.grid() actbtn = Button(win, width=8, text="添加工时", command=inp) actbtn.grid() clrbtn = Button(win, text="清空所有", command=clear) clrbtn.grid() delbtn = Button(win, width=8, text="删除最后一条", command=pop) delbtn.grid() bttt = Button(win, width=12, text="计算加班", command=show) bttt.grid(padx=23) win.mainloop()
关键改动说明:
- 格式转换逻辑:新增
hhmm_to_float函数,拆分HH:MM字符串为小时和分钟,计算为浮点小时数并保留两位小数,完全匹配你给出的示例转换结果。 - 输入验证:加入空输入、格式错误的判断,用弹窗提示用户,避免程序因非法输入崩溃。
- 显示优化:同时展示原始HH:MM输入和转换后的浮点小时数,让输入和计算结果的对应关系更清晰。
- 加班计算优化:区分总工时是否超过40小时,分别显示对应信息,加班计算结果包含总工时、加班时长和加班薪资,信息更完整。
- 细节调整:加宽输入框适配HH:MM格式,更新标签提示文本,添加输入后自动清空功能,提升使用体验。
内容的提问来源于stack exchange,提问作者Abdo Proff
相关产品推荐
相关产品推荐

