所以我一直在编写一些即将完成的代码。它是一个简单的 GUI 程序,可根据输入动态运行特定方程。感谢本网站(二战)上的一位用户,我能够在他提供的代码中使用自定义数字输入格式。我需要做的最后一件事就是将他的代码实现到我的代码中,让它们一起工作。在我的代码中,有 4 个输入框,其中两个指的是线上的特定位置(这是输入自定义数字格式的位置),另外两个指的是从线上的这些位置到同一点的测量值。当所有盒子都有数据后,会显示两个结果。一个结果是与该线成直角的线的长度。第二个结果是在线上放置它的确切位置(这是自定义数字格式输出的位置)。
例如,如果我将测量位置放在 120 和 140 线上,分别从这些位置 10 和 15 进行测量,则得到的结果为 7.262(与线成直角的线的长度)测量结果为 126.875。使用 wwii 帮助我编写的代码,我希望输入框 120 和 140 以及输出框 126.875 使用数字格式,如下所示:1+20.00、1+40.00 和 1+26.88。
代码 wwii 帮助我直接转换这些数字格式,但我希望在输入 1+20 时读取这些输入字段并将其转换,并让计算结果显示在自定义中格式也是如此。
我知道这很啰嗦,但是,我希望它有意义。这是我的代码的副本(不是很长)以及二战帮助我的代码。它不喜欢我分成单独的代码框,因此二战帮助我的代码添加在代码块的底部。
from tkinter import *
from math import *
def show_entry_fields():
try:
a, c, d, e, = float(e1.get()), float(e3.get()), float(e4.get()), float(e5.get())
b = e - d
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))) + d)
side = float(format(side, '.3f'))
side_label['text'] = str(side)
except ValueError:
pass
master.after(100, show_entry_fields)
master = Tk()
master.attributes("-topmost", True)
master.title("Triangulation Plotting")
Label(master, text="Measurement #1 Station Line Location").grid(row=1, column=0, sticky=W, pady=4)
e4 = Entry(master)
e4.grid(row=1, column=1, sticky=E)
Label(master, text="Triangulation Measurement #1").grid(row=2, column=0, sticky=W, pady=4)
e1 = Entry(master)
e1.grid(row=2, column=1, sticky=E)
Label(master, text="Measurement #2 Station Line Location").grid(row=3, column=0, sticky=W, pady=4)
e5 = Entry(master)
e5.grid(row=3, column=1, sticky=E)
Label(master, text="Triangulation Measurement #2").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="Measurement on Station Line").grid(row=9, column=0, sticky=W, pady=4)
side_label = Label(master, text="")
side_label.grid(row=9, column=1)
master.after(100,show_entry_fields)
master.mainloop()
Custom number format code from wwii:
def convert(thing):
if isinstance(thing, str):
a,b = thing.split('+')
b,*d = b.split('.')
d = '00' if not d else d[0]
thing = round(float(f'{a}{b}.{d}'), 2)
elif isinstance(thing, (int,float)):
thing = str(round(thing, 2))
thing,*d = thing.split('.')
d = '00' if not d else d[0]
thing = thing if len(thing) > 2 else '0'+thing
thing = f'{thing[:-2]}+{thing[-2:]}.{d}'
return thing
提前感谢您的帮助。
最佳答案
因此 wwii 为您提供的代码可以在格式之间进行转换。因此,您无需对代码进行太多操作即可使其正常工作。首先,将 wwii 中的代码移至程序的开头。然后,您只需更改函数 show_entry_fields
中的 2 行即可.
第一个是:a, c, d, e = float(e1.get()), float(e3.get()), float(e4.get()), float(e5.get())
您必须将其更改为:a, c, d, e = float(e1.get()), float(e3.get()), convert(str(e4.get())), convert(str(e5.get()))
第二个是:side = float(format(side, '.3f'))
您必须将其更改为:side = convert(float(format(side, '.3f')))
完成此操作后,您可以在测量 #1 车站线路位置和测量 #2 车站线路位置附近的条目中输入 1+20 和 1+40,然后您将在站线测量中获取转换结果。
所有这一切的主要思想是在输入之后和任何计算之前进行转换(因此第一行中的变化),并在所有计算之后但输出显示之前进行转换(因此第一行中的变化)第二行)。
这是整段代码:
from tkinter import *
from math import *
# Custom number format code from wwii:
def convert(thing):
if isinstance(thing, str):
a,b = thing.split('+')
b,*d = b.split('.')
d = '00' if not d else d[0]
thing = round(float(f'{a}{b}.{d}'), 2)
elif isinstance(thing, (int,float)):
thing = str(round(thing, 2))
thing,*d = thing.split('.')
d = '00' if not d else d[0]
thing = thing if len(thing) > 2 else '0'+thing
thing = f'{thing[:-2]}+{thing[-2:]}.{d}'
return thing
def show_entry_fields():
try:
a, c, d, e = float(e1.get()), float(e3.get()), convert(str(e4.get())), convert(str(e5.get()))
b = e - d
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))) + b)
side = convert(float(format(side, '.3f')))
side_label['text'] = str(side)
except ValueError:
pass
master.after(100, show_entry_fields)
master = Tk()
master.attributes("-topmost", True)
master.title("Triangulation Plotting")
Label(master, text="Measurement #1 Station Line Location").grid(row=1, column=0, sticky=W, pady=4)
e4 = Entry(master)
e4.grid(row=1, column=1, sticky=E)
Label(master, text="Triangulation Measurement #1").grid(row=2, column=0, sticky=W, pady=4)
e1 = Entry(master)
e1.grid(row=2, column=1, sticky=E)
Label(master, text="Measurement #2 Station Line Location").grid(row=3, column=0, sticky=W, pady=4)
e5 = Entry(master)
e5.grid(row=3, column=1, sticky=E)
Label(master, text="Triangulation Measurement #2").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="Measurement on Station Line").grid(row=9, column=0, sticky=W, pady=4)
side_label = Label(master, text="")
side_label.grid(row=9, column=1)
master.after(100, show_entry_fields)
master.mainloop()
关于python - 在 tkinter 脚本中实现自定义数字代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54566405/