from tkinter import *
import math
def calc(event=None):
try:
x1 = float(et1.get().replace(" ", ""))
y1 = float(et2.get().replace(" ", ""))
x2 = float(et3.get().replace(" ", ""))
y2 = float(et4.get().replace(" ", ""))
result = math.sqrt(math.pow((y2-y1), 2) + math.pow((x2-x1), 2))
var.set(result)
except ValueError:
pass
def delete(event=None):
global et1, et2, et3, et4
et1.delete(0, END)
et2.delete(0, END)
et3.delete(0, END)
et4.delete(0, END)
et1.focus()
var.set(0)
root = Tk()
root.title("计算距离")
root.geometry("415x102")
root.resizable(0, 0)
root.bind("<KeyPress-Return>", calc)
root.bind("<Control-d>", delete)
var = IntVar()
# x1
Label(root, text="x1坐标:").grid(row=0, column=0)
et1 = Entry(root)
et1.grid(row=0, column=1)
et1.focus()
# y1
Label(root, text="y1坐标:").grid(row=0, column=2)
et2 = Entry(root)
et2.grid(row=0, column=3)
# x2
Label(root, text="x2坐标:").grid(row=1, column=0)
et3 = Entry(root)
et3.grid(row=1, column=1)
# y2
Label(root, text="y2坐标:").grid(row=1, column=2)
et4 = Entry(root)
et4.grid(row=1, column=3)
# result
Label(root, text="计算结果:").grid(row=2, column=0)
et5 = Entry(root, state="readonly", textvariable=var)
et5.grid(row=2, column=1)
# 计算
Button(root, text="计算", width=10, command=calc).grid(row=3, column=0, columnspan=2)
# 复制
Button(root, text="清空", width=10, command=delete).grid(row=3, column=2, columnspan=3)
mainloop()