自动将新行上的所有值加在一起并显示在输入字段中
Automatically add all values on new lines together and show them in a entry field
我正在使用 Tkinter 开发一个应用程序,我需要它在文本输入字段中自动添加所有由换行符 (\n) 分隔的浮点值,并在另一个字段中显示所有浮点数相加的结果输入字段。我附上了程序的图像。
Image of the Program
这是我目前的代码。
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()
您可以将数字附加到列表中(如果它只是添加和删除)。然后创建一个函数,将列表中的所有数字加在一起
例如
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
一种可能的 pythonic one liner:
total = sum(map(float, text_field.split())
我正在使用 Tkinter 开发一个应用程序,我需要它在文本输入字段中自动添加所有由换行符 (\n) 分隔的浮点值,并在另一个字段中显示所有浮点数相加的结果输入字段。我附上了程序的图像。 Image of the Program
这是我目前的代码。
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()
您可以将数字附加到列表中(如果它只是添加和删除)。然后创建一个函数,将列表中的所有数字加在一起
例如
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
一种可能的 pythonic one liner:
total = sum(map(float, text_field.split())