I have written a small bit of code to tell me how many hours I need to work to finish up my 40 hours a week each day.
I can add hours per day and it will calculate total hours left in the work week.
My main issue with it is the way my math is written. It just seems so complicated and unnecessary but I was unable to find a better solution and was hoping someone could point me in the right direct for a better formula.
If you check the hours_left_update(self)
method you will see that I am using floats, the Decimal method with str and quantize as well as split. All to try and get an output to a label that shows my results in hours and minutes.
Keep in mind when you put in hours worked the time clock system I get my information from uses a decimal system to display work hours. So if I worked 7 and 1/2 hours one day it would show me 7.50 hours worked. This is the kind of format I am putting into the program and it automatically convert that number to display as 7 hours 30 min.
from decimal import Decimal, ROUND_HALF_UP
from time import strftime
import tkinter as tk
import datetime
class TimeClockApp(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.hours_worked = 0.00
self.dtlbl = tk.Label(self.master, text="Current Date-Time: {} ~ {}".format(datetime.date.today(),strftime("%H:%M:%S")))
self.dtlbl.grid(row=0, column=0, columnspan=2)
self.lbl1 = tk.Label(self.master, text="Hours worked").grid(row=1, column=0, columnspan=2)
weekdays = ["Monday: ", "Tuesday: ", "Wednesday: ", "Thursday: ", "Friday: "]
for i in range(5):
tk.Label(self.master, text=weekdays[i], anchor = "e").grid(row=i+2, column=0, sticky="e")
self.lbl7 = tk.Label(self.master, text="Total Hours worked:").grid(row=7, column=0, sticky="e")
self.lbl8 = tk.Label(self.master, text="00h 0m")
self.lbl8.grid(row=7, column=1, sticky="w")
self.lbl9 = tk.Label(self.master, text="Hours left to 40:").grid(row=8, column=0, sticky="e")
self.lbl10 = tk.Label(self.master, text="40h 0m")
self.lbl10.grid(row=8, column=1, sticky="w")
self.entry_list = []
for i in range(5):
self.entry_list.append(tk.Entry(self.master,width=7))
self.entry_list[i].bind("<Key>", lambda x: self.master.after(500, self.hours_left_update))
self.entry_list[i].grid(row = i+2, column = 1, sticky = "w")
self.status_clock()
def status_clock(self):
self.dtlbl.config(text ="Current Date-Time: {} ~ {}".format(datetime.date.today(),strftime("%H:%M:%S")))
self.dtlbl.after(1000, lambda: self.status_clock())
def hours_left_update(self):
for entry in self.entry_list:
if entry.get() != "":
try:
#print(Decimal(entry.get()).quantize(Decimal("0.01"), ROUND_HALF_UP))
self.hours_worked += float(entry.get())
except:
pass
x = str(Decimal(str(self.hours_worked)).quantize(Decimal("0.01"), ROUND_HALF_UP)).split('.')
m60 = "00"
if int(x[1]) != 0:
m60 = Decimal(str(60 * float("0.{}".format(x[1])))).quantize(Decimal("1"))
self.lbl8.config(text="{}h {}m".format(x[0], m60))
var = 40 - int(x[0])
if int(x[1]) != 0:
var = 39 - int(x[0])
x2 = "00"
if Decimal(str(60 * float("0.{}".format(x[1])))).quantize(Decimal("1")) != 0:
x2 = (60 - Decimal(str(60 * float("0.{}".format(x[1])))).quantize(Decimal("1")))
self.lbl10.config(text="{}h {}m".format(var,x2))
self.hours_worked = 0.00
if __name__ == "__main__":
root = tk.Tk()
app = TimeClockApp(root)
root.mainloop()