通过导入EXCEL文件,读取sheet和columns信息,计算经纬度距离。重点是通过调用读取的sheet名、columns名去完成数据读取。
import tkinter
from tkinter import ttk
from tkinter import *
from tkinter import filedialog
import pandas as pd
from math import *
import datetime
import tkinter.messagebox
def openfile():
# 定义字典,存放EXCEL的sheet名、columns名
dic = {}
path_ = filedialog.askopenfilename(title='打开文件')
if path_ == "":
pass
else:
var_path.set(path_)
df = pd.read_excel(var_path.get(), sheet_name=None)
for sht_names in df.keys():
df_tmp = pd.read_excel(var_path.get(), sheet_name=sht_names)
col_names = list(df_tmp.columns.values)
dic[sht_names] = col_names
# print(dic)
# 设置第一组、第二组下拉框的内容
com11['value'] = list(dic.keys())
com11.current(0)
com21['value'] = list(dic.keys())
com21.current(0)
# 定义调用方法,供第一组的第一个下拉框变化时调用
def com11_bind_com(event):
com12.delete(0, tkinter.END)
value = var11.get()
com12['value'] = dic[value]
com12.current(0) # 设置默认值
com13['value'] = dic[value]
com14['value'] = dic[value]
# 定义调用方法,供第二组的第一个下拉框变化时调用
def com21_bind_com(event):
com22.delete(0, tkinter.END)
value = var21.get()
com22['value'] = dic[value]
com22.current(0) # 设置默认值
com23['value'] = dic[value]
com24['value'] = dic[value]
# 第一组与第二组、它们的第一个下拉框以及后面的下拉框绑定
com11.bind("<>", com11_bind_com)
com21.bind("<>", com21_bind_com)
#定义计算经纬度距离的公式
def get_distance(lon1, lat1, lon2, lat2):
lon1, lat1, lon2, lat2 = map(radians, [float(lon1), float(lat1), float(lon2), float(lat2)])
d_lon, d_lat = lon2 - lon1, lat2 - lat1
a = sin(d_lat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(d_lon / 2) ** 2
distance = 2 * asin(sqrt(a)) * 6371229
return float('%.2f' % distance)
def process():
dfa = pd.read_excel(var_path.get(), sheet_name=com11.get())
dfb = pd.read_excel(var_path.get(), sheet_name=com21.get())
result = []
for siteA, lonA, latA in zip(dfa[com12.get()], dfa[com13.get()], dfa[com14.get()]):
for siteB, lonB, latB in zip(dfb[com22.get()], dfb[com23.get()], dfb[com24.get()]):
get_dist = get_distance(lonA, latA, lonB, latB)
result.append([siteA, siteB, get_dist])
dfx = pd.DataFrame(result)
dfx.rename(columns={0: '基站a', 1: '基站b', 2: '距离(米)'}, inplace=True)
outfile = ''
str1 = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S-%f')
while outfile == '':
outfile = filedialog.asksaveasfilename(title='保存文件', filetypes=[('EXCEL', '.xlsx')], initialfile="01")
outfile = outfile + "-" + str1 + ".xlsx"
dfx.to_excel(outfile, encoding="gbk", index=None)
tkinter.messagebox.showinfo(title='message', message='完成!!!')
if __name__ == "__main__":
win = Tk()
win.title("hhn_寒号鸟")
win.geometry("500x300")
# 文件路径变量
var_path = StringVar()
en_1 = Entry(win, textvariable=var_path, width=45, fg='gray').place(x=20, y=10)
bt_2 = Button(win, text="导 入", width=12, command=lambda: openfile()).place(x=380, y=6)
la_11 = Label(win, text='sheet 1 关键字段 1 经度 1'
' 纬度 1').place(x=20, y=60)
# 创建第一个下拉框和他的变量
var11 = StringVar()
com11 = ttk.Combobox(win, textvariable=var11, state='readonly', width=10, height=10)
com11.place(x=20, y=80)
# 创建第二个下拉框和他的变量
var12 = StringVar()
com12 = ttk.Combobox(win, textvariable=var12, state='readonly', width=10, height=10)
com12.place(x=140, y=80)
# 创建第三个下拉框和他的变量
var13 = StringVar()
com13 = ttk.Combobox(win, textvariable=var13, state='readonly', width=10, height=10)
com13.place(x=260, y=80)
# 创建第四个下拉框和他的变量
var14 = StringVar()
com14 = ttk.Combobox(win, textvariable=var14, state='readonly', width=10, height=10)
com14.place(x=380, y=80)
la_21 = Label(win, text='sheet 2 关键字段 2 经度 2'
' 纬度 2').place(x=20, y=120)
# 创建第五个下拉框和他的变量
var21 = StringVar()
com21 = ttk.Combobox(win, textvariable=var21, state='readonly', width=10, height=10)
com21.place(x=20, y=140)
# 创建第六个下拉框和他的变量
var22 = StringVar()
com22 = ttk.Combobox(win, textvariable=var22, state='readonly', width=10, height=10)
com22.place(x=140, y=140)
# 创建第七个下拉框和他的变量
var23 = StringVar()
com23 = ttk.Combobox(win, textvariable=var23, state='readonly', width=10, height=10)
com23.place(x=260, y=140)
# 创建第八个下拉框和他的变量
var24 = StringVar()
com24 = ttk.Combobox(win, textvariable=var24, state='readonly', width=10, height=10)
com24.place(x=380, y=140)
bt_3 = Button(win, text="运 行", width=13, command=lambda: process()).place(x=100, y=250)
bt_4 = Button(win, text="退 出", width=13, command=win.quit).place(x=300, y=250)
win.mainloop()