I was working on an app in Tkinter and I needed it to automatically add all float values that are separated by new lines (\n) in a text input field and display the result of all the floats added together in another entry field. I have attached an image of the program. Image of the Program
Here is the code I have so far.
from tkinter import *
window = Tk()
window.title("BFCalc")
window.geometry("500x875")
E1Val = StringVar()
E1_value = StringVar()
E2_value = StringVar()
E3_value = StringVar()
def CalculateNum():
LengthVal = float(E1_value.get())
WidthVal = float(E2_value.get())
ThicknessVal = float(E3_value.get())
FinalCalc = LengthVal * WidthVal * ThicknessVal / 144
FinalCalc = round(FinalCalc,2)
E4.delete(0,"end")
E4.insert(END,FinalCalc)
T1.insert(END,f'{FinalCalc}\n')
def ClearVal():
E1.delete(0,"end")
E2.delete(0,"end")
E3.delete(0,"end")
E4.delete(0,"end")
E3.insert(0,'1')
L1 = Label(text="Board Foot Calculator",font='Helvetica 20 bold underline')
L1.pack()
L2 = Label(text="Input",font='Helvetica 16 bold underline')
L2.pack()
L3 = Label(text="Length",font='Helvetica 14 bold italic')
L3.pack()
E1 = Entry(window,font='Goudy 14 bold',textvariable=E1_value)
E1.pack()
L4 = Label(text="Width",font='Helvetica 14 bold italic')
L4.pack()
E2 = Entry(window,font='Goudy 14 bold',textvariable=E2_value)
E2.pack()
L5 = Label(text="Thickness",font='Helvetica 14 bold italic')
L5.pack()
E3 = Entry(window,font='Goudy 14 bold',textvariable=E3_value)
E3.insert(0,'1')
E3.pack()
B1 = Button(window,text="Calculate",font='Helvetica 14 bold',height=2,width=15,command=CalculateNum)
B1.pack(pady=40)
L6 = Label(text="Board Feet",font='Helvetica 16 bold italic')
L6.pack()
E4 = Entry(window,font='Goudy 14 bold')
E4.pack()
B2 = Button(window,text="Clear",font='Helvetica 14 bold',height=2,width=10,command=ClearVal)
B2.pack(pady=40)
L7 = Label(text="",font='Goudy 12')
L7.pack()
T1 = Text(window,width=10,height=40,font='Goudy 12')
T1.place(x=400,y=50)
window.mainloop()
You could append the numbers to a list (if it is just adding and taking away). Then create a function that adds all the numbers in the list together
e.g.
answers = []
def CalculateNum():
LengthVal = float(E1_value.get())
WidthVal = float(E2_value.get())
ThicknessVal = float(E3_value.get())
FinalCalc = LengthVal * WidthVal * ThicknessVal / 144
FinalCalc = round(FinalCalc,2)
answers.append(FinalCalc)
E4.delete(0,"end")
E4.insert(END,FinalCalc)
T1.insert(END,f'{FinalCalc}\n')
Total() #Goes to add up the total
def Total():
tot = 0 # The total number
for num in answers:
tot += num # Adds all the numbers in the list to the total
#print(tot)
#Then insert the number where you want it to go