python,基于tkinter模块编写的根据经纬度计算两点间距离的应用程序

python的tkinter模块是用于编写GUI窗口程序的模块,使用起来非常方便,功能强大。基于tkinter模块,开发了一个输入两点经纬度计算输出距离(包括公里数和孤度数)的小程序,主要应用于地震台站地震报告编写。下面这段代码可以在python3.8上直接运行。

import tkinter as tk
import tkinter.messagebox
from math import radians, cos, sin, asin, sqrt

#定义由输入文本框获得台站及震源经纬度,计算距离公里数及孤度数,并在输出文本框text中输出的函数
def haversine(): # 经度1,纬度1,经度2,纬度2


    distance_km.delete('0.0',tk.END)
    distance_angle.delete('0.0',tk.END)
    lon1 = float(sta_longitude.get())
    lat1 = float(sta_latitude.get())
    lon2 = float(source_longitude.get())
    lat2 = float(source_latitude.get())
    # 将十进制度数转化为弧度
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine公式
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    c = 2 * asin(sqrt(a))
    r = 6371  # 地球平均半径,单位为公里
    km = round(c * r, 2) # 公里数
    angle = round(c * r / 111.199, 2) # 孤度数
    distance_angle.insert('insert', angle) # 在公里数文本框中输出计算结果孤度数
    distance_km.insert('insert', km) # 在孤度数文本框中输出计算结果公里数



window = tk.Tk()
window.title('吉林省长白山天池火山监测站地震分析专用')
window.geometry('600x400')
# 定义第1行内容
label1 = tk.Label(window, text = '台站纬度', font = ('微软雅黑', 15))
label1.place(x=20, y=30, anchor='nw')
# 台站纬度设置成默认值
sta_lat = tk.StringVar()
sta_lat.set('42.0576')
sta_latitude = tk.Entry(window, font = ('微软雅黑', 15), width = 10, textvariable = sta_lat)
sta_latitude.place(x=120, y=30, anchor='nw')
label2 = tk.Label(window, text = '台站经度', font = ('微软雅黑', 15))
label2.place(x=300, y=30, anchor='nw')
# 台站经度设置成默认值
sta_lon = tk.StringVar()
sta_lon.set('128.0626')
sta_longitude = tk.Entry(window, font = ('微软雅黑', 15), width = 10, textvariable = sta_lon)
sta_longitude.place(x=400, y=30, anchor='nw')
# 定义第2行内容
label3 = tk.Label(window, text = '震源纬度', font = ('微软雅黑', 15))
label3.place(x=20, y=110, anchor='nw')
source_latitude = tk.Entry(window, font = ('微软雅黑', 15), width = 10)
source_latitude.place(x=120, y=110, anchor='nw')
label4 = tk.Label(window, text = '震源经度', font = ('微软雅黑', 15))
label4.place(x=300, y=110, anchor='nw')
source_longitude = tk.Entry(window, font = ('微软雅黑', 15), width = 10)
source_longitude.place(x=400, y=110, anchor='nw')
# 定义第3行内容
label5 = tk.Label(window, text = '孤度数', font = ('微软雅黑', 15))
label5.place(x=20, y=190, anchor='nw')
distance_angle = tk.Text(window, font = ('微软雅黑', 15), width = 10, fg = 'red', height = 1)
distance_angle.place(x=120, y=190, anchor='nw')
label6 = tk.Label(window, text = '公里数', font = ('微软雅黑', 15))
label6.place(x=300, y=190, anchor='nw')
distance_km = tk.Text(window, font = ('微软雅黑', 15), width = 10, fg = 'red', height = 1)
distance_km.place(x=400, y=190, anchor='nw')

# 定义第4行内容
calculate = tk.Button(window, text = '开始计算', font = ('微软雅黑', 15), command = haversine)

calculate.place(x=150, y=270, anchor='nw')
close_window = tk.Button(window, text = '关闭窗口', font = ('微软雅黑', 15),  command = window.quit)
close_window.place(x=350, y=270, anchor='nw')

# 消息循环,显示窗口
window.mainloop()

运行并输入参数后显示如下:

猜你喜欢

转载自www.cnblogs.com/iceberg710815/p/12274370.html
今日推荐