Take user input in HH:MM and convert it to a float

Question:

I am trying to create a program where I add number of hours worked into a list then sum inputs and calculate overtime for hours worked above 40

from tkinter import *


#Tkinter window
win = Tk()
win.geometry("500x500")
win.title("Attempt")
win.resizable(False,False)
my_list =[]


#Get input(shift hours)
def inp():
    
    for _ in range(1):
        my_list.append(float(actinp.get()))
        
        showlabel.config(text=((my_list)))

#Calculate overtime for +40 hours
def show():
    rate =(int(rateinp.get()))
    a40=float(sum(my_list)-40)
    showlabel.config(text=(a40*rate*0.5))

#Clear the list/screen
def clear ():
    my_list.clear()
    showlabel.config(text=" ")

#Remove the last input from list/screen
def pop():
    my_list.pop()        
    showlabel.config(text=(my_list))


#Tkinter widgets
kk=Label(win, width=70, height=1)
kk.grid()
showlabel=Label(win, width=71, height=10, bg='white')
showlabel.grid()
actlab=Label(win, text="Number of hours")
actlab.grid()
actinp=Entry(win, width=5)
actinp.grid()
ratelab=Label(win,text="Rate")
ratelab.grid()
rateinp=Entry(win,width=5)
rateinp.grid()
actbtn=Button(win, width=5, text="Add", command=inp)
actbtn.grid()
clrbtn=Button(win,text="Clear",command=clear)
clrbtn.grid()
delbtn=Button(win,width=5, text="Delete", command=pop)
delbtn.grid()
bttt=Button(win, width=10, text="Calculate", command=show)
bttt.grid(padx=23)




win.mainloop()

I am only able to do this by taking a float input, but I need the input to be collected in HH:MM format, then switch it to a float to use it with the math in the other function.

example:
Input: [11:30, 4:42, 5:07] – switch to: [11.5, 4.70, 5.13]

Asked By: Abdo Proff

||

Answers:

Suppose you have a list:

l=['11:30', '4:42', '5:07']

k=[x.split(':') for x in l]  # split elements on ':'

#[['11', '30'], ['4', '42'], ['5', '07']]

Now, convert the first part to float:

m =[float(x[0]) for x in k]

#[11.0, 4.0, 5.0]

Convert the minutes to fraction by dividing by 60:

n=[float(x[1])/60 for x in k]

#[0.5, 0.7, 0.11666666666666667]

Finally add them back:

[x+y for x,y in zip(m,n)]

#[11.5, 4.7, 5.116666666666666]
Answered By: God Is One

Try this.

Code:

def inp():
    
    #for _ in range(1):
    #my_list.append(float(actinp.get()))
    x= actinp.get()
    r = x.replace(':', '.')
    my_list.insert(0,r)
    print('x:' , my_list)
    
    showlabel.config(text=((my_list)))
    actinp.delete(0, END)

Screenshot:

enter image description here

Answered By: toyota Supra

To convert input "HH:MM" (actinp.get()) to float number (hours):

  • split the string using h, m = actinp.get().strip().split(":")
  • calculate the hours using hours = int(h) + round(int(m)/60, 2)

Below is the updated inp():

def inp():
    try:
        # get the hours and minutes in string
        h, m = actinp.get().strip().split(":")
        # convert the input hours and minutes to hours in float number
        hours = int(h) + round(int(m)/60, 2)
        my_list.append(hours)
    except ValueError as ex:
        print(ex)
    showlabel.config(text=my_list)
Answered By: acw1668
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.