首先让我说我对编码非常陌生。我拼凑而成,并编写了一些代码,这些代码根据在3个Entry字段中输入的数据来评估方程式。我有它,所以当我点击“计算”时,它会计算方程式并给出答案。有没有一种方法可以让它自动计算,这样我就不必在每组输入后都点击“计算”?理想情况下,用户只需选择,输入数据,选项卡,输入数据,选项卡,输入数据,然后使用给出的答案即可。然后,他们将通过跳回到初始输入字段来重复该过程。
下面是我的代码的副本:
from tkinter import *
from math import *
def show_entry_fields():
a, b, c = float(e1.get()), float(e2.get()), float(e3.get())
s = (a + b + c) / 2
height = (sqrt (s * (s - a) * (s - b) * (s - c)) * 2) / b
height = float(format(height, '.3f'))
height_label['text'] = str(height)
side =(sqrt ((a ** 2) - (height ** 2)))
side = float(format(side, '.3f'))
side_label['text'] = str(side)
master = Tk()
master.attributes("-topmost", True)
Label(master, text="Measurement from nearest to 0+00").grid(row=5, column=0, sticky=W, pady=4)
e1 = Entry(master)
e1.grid(row=5, column=1, sticky=E)
Label(master, text="Distance between points on Station Line").grid(row=6, column=0, sticky=W, pady=4)
e2 = Entry(master)
e2.grid(row=6, column=1, sticky=E)
Label(master, text="Measurement from farthest from 0+00").grid(row=7, column=0, sticky=W, pady=4)
e3 = Entry(master)
e3.grid(row=7, column=1, sticky=E, pady=4)
Label(master, text="Offset from station line").grid(row=8, column=0, sticky=W, pady=4)
height_label = Label(master, text="")
height_label.grid(row=8, column=1)
Label(master, text="Feet from Measurement nearest to 0+00").grid(row=9, column=0, sticky=W, pady=4)
side_label = Label(master, text="")
side_label.grid(row=9, column=1)
Button(master, text='Calculate', command=show_entry_fields).grid(row=10, column=0, pady=4)
mainloop()
答案 0 :(得分:0)
有几种方法可以执行此操作,但是由于执行的数学非常简单,因此我建议使用root.after
来计算结果。基本上,它将尝试每0.1秒计算一次,如果没有发现错误,则显示结果。
from tkinter import *
from math import *
def show_entry_fields():
try:
a, b, c = float(e1.get()), float(e2.get()), float(e3.get())
s = (a + b + c) / 2
height = (sqrt(s * (s - a) * (s - b) * (s - c)) * 2) / b
height = float(format(height, '.3f'))
height_label['text'] = str(height)
side = (sqrt((a ** 2) - (height ** 2)))
side = float(format(side, '.3f'))
side_label['text'] = str(side)
except ValueError: #catch empty field or invalid input error
pass
master.after(100, show_entry_fields)
...
master.after(100,show_entry_fields) # add this
master.mainloop()