需要使用Tkinter和Python为km或mi创建选项



因此,下面我找到了一个代码,并对其进行了修改,用于计算两个位置之间的距离。我正在为简单的用户输入创建一个GUI。一切都按预期进行,但如果我能完成两件事,那将是一个额外的收获。

  1. 有一个切换或选择,允许我以KM或Mi 显示距离

  2. 有什么办法可以把输出四舍五入,这样小数后就不会有那么多数字了吗?

我假设它必须是基于对象选择的某种类型的变量?

# import modules 
from tkinter import *
from geopy.geocoders import Nominatim 
from geopy import distance 
# user defined funtion 
def get_dis(): 
try: 

geolocator = Nominatim(user_agent="geoapiExercises") 

place1 = geolocator.geocode(str(e1.get())) 
place2 = geolocator.geocode(str(e2.get())) 

Loc1_lat,Loc1_lon = (place1.latitude),(place1.longitude) 
Loc2_lat,Loc2_lon = (place2.latitude),(place2.longitude) 
location1=(Loc1_lat,Loc1_lon) 
location2=(Loc2_lat,Loc2_lon) 
res = (str(distance.distance(location1, location2).mi)+" Mi") 
result.set(res) 
except: 
result.set("someting went wrong") 
# object of tkinter 
# with background set to light grey 
master = Tk() 
master.configure(bg='light grey') 
master.title("Find Distance") 
# Variable Classes in tkinter 
result = StringVar(); 

# Creating label for each information 
# name using widget Label 
Label(master, text="Start Location : " , bg = "light grey").grid(row=1, sticky=W) 
Label(master, text="End Location : " , bg = "light grey").grid(row=2, sticky=W) 
Label(master, text="Result :", bg = "light grey").grid(row=3, sticky=W) 
# Creating label for class variable 
# name using widget Entry 
Label(master, text="", textvariable=result,bg = "light grey").grid(row=3,column=1, sticky=W) 

e1 = Entry(master,width = 50) 
e1.grid(row=1, column=1) 
e2 = Entry(master,width = 50) 
e2.grid(row=2, column=1) 
# creating a button using the widget 
b = Button(master, text="Check", command=get_dis, bg = "white") 
b.grid(row=1, column=2,columnspan=2, rowspan=2,padx=5, pady=5,) 
mainloop()

使用注释进行编辑。。无法绕过

从tkinter导入模块*从geopy.geocoders从geopy导入Nomatim导入距离

用户定义的函数定义get_dis((:

try: 

geolocator = Nominatim(user_agent="geoapiExercises") 

place1 = geolocator.geocode(str(e1.get())) 
place2 = geolocator.geocode(str(e2.get())) 

Loc1_lat,Loc1_lon = (place1.latitude),(place1.longitude) 
Loc2_lat,Loc2_lon = (place2.latitude),(place2.longitude) 
location1=(Loc1_lat,Loc1_lon) 
location2=(Loc2_lat,Loc2_lon) 
res = (str(distance.distance(location1, location2) .km)+" Km") 
result.set(res) 
except: 
result.set("someting went wrong") 
def get_dis():
if unit.get() == 'mi':
res = str(round(distance.distance(location1, location2).mi, 2))+" Mi"
else:
res = str(round(distance.distance(location1, location2).km, 2))+" km"   

tkinter对象

背景设置为浅灰色master=Tk((master。configure(bg="浅灰色"(master。title("查找距离"(

tkinter-result=StringVar((中的变量类;标签(主,text=",textvariable=结果,bg="浅灰色"(。网格(行=4,列=1

粘性=W(

为每个信息创建标签

名称使用小部件标签标签(主,文本="开始位置:",bg="浅灰色"(.网格(行=1,粘性=W(标签(主、文本="结束

位置:",bg=";浅灰色(。grid(行=2,粘性=W(
标签(主,文本="距离:",bg="浅灰色"(.grid(行=3,粘性=W(

正在为类变量创建标签

名称使用小部件入口标签(主,文本="单位:",bg="浅灰色"(。网格(行=3,粘性=W(标签(主、文本="结果:",bg=

"浅灰色(。网格(行=4,粘性=W(e1=条目(主条目,宽度=50(e1.网格(行=1,列=1(e2=条目(主机条目,宽度=50(e2.网格(列=2,行=1(

f1=框架(主(f1.网格(行=3,列=1,粘性=W(单位=StringVar(value="mi"(单选按钮(f1,text="英里",value="米";,变量=单位,bg=";浅灰色"(。pack(side="left"(单选按钮(f1,text=";km";,值=";km";,变量=单位,bg=";光灰色"(。包装(侧面="左"(

使用小部件b=button(master,text="Calculate",command=get_dis,bg="white"(b.grid(row=1

column=2,columnspan=2,rowspan=2,padx=5,pady=5,(mainloop((

首先在结果行之前添加两个单选按钮:

Label(master, text="Unit :", bg="light gray").grid(row=3, sticky=W)
Label(master, text="Result :", bg = "light grey").grid(row=4, sticky=W) # changed to row 4
...
f1 = Frame(master)
f1.grid(row=3, column=1, sticky=W)
unit = StringVar(value="mi")
Radiobutton(f1, text="miles", value="mi", variable=unit, bg="light gray").pack(side="left")
Radiobutton(f1, text="km", value="km", variable=unit, bg="light gray").pack(side="left")
result = StringVar(); 
Label(master, text="", textvariable=result,bg = "light grey").grid(row=4,column=1, sticky=W) # changed to row 4

然后根据get_dis():内部选择的单元调用相应的函数

def get_dis():
try:
...
if unit.get() == 'mi':
res = str(round(distance.distance(location1, location2).mi, 3))+" Mi"
else:
res = str(round(distance.distance(location1, location2).km, 3))+" km"
...

注意,round(..., 3)用于将结果四舍五入到小数点后有3位数字。

# import modules 
from tkinter import *
from geopy.geocoders import Nominatim 
from geopy import distance 

# user defined funtion 
def get_dis(): 


geolocator = Nominatim(user_agent="geoapiExercises") 

place1 = geolocator.geocode(str(e1.get())) 
place2 = geolocator.geocode(str(e2.get())) 

Loc1_lat,Loc1_lon = (place1.latitude),(place1.longitude) 
Loc2_lat,Loc2_lon = (place2.latitude),(place2.longitude) 
location1=(Loc1_lat,Loc1_lon) 
location2=(Loc2_lat,Loc2_lon) 
res = (str(distance.distance(location1, location2) .km)+" Km") 
if unit.get() == 'mi':
res = str(round(distance.distance(location1, location2).mi, 2))+" Mi"
else:
res = str(round(distance.distance(location1, location2).km, 2))+" km"
result.set(res) 

# object of tkinter 
# with background set to light grey 
master = Tk() 
master.configure(bg='light grey') 
master.title("Find Distance") 

# Variable Classes in tkinter 
result = StringVar(); 
Label(master, text="", textvariable=result,bg = "light grey").grid(row=4,column=1, sticky=W)  

# Creating label for each information 
# name using widget Label  
Label(master, text="Start Location : " , bg = "light grey").grid(row=1, sticky=W) 
Label(master, text="End Location : " , bg = "light grey").grid(row=2, sticky=W) 


# Creating label for class variable 
# name using widget Entry 
Label(master, text="Unit :", bg="light gray").grid(row=3, sticky=W)
Label(master, text="Result :", bg = "light grey").grid(row=4, sticky=W) 


e1 = Entry(master,width = 50) 
e1.grid(row=1, column=1) 
e2 = Entry(master,width = 50) 
e2.grid(row=2, column=1) 
f1 = Frame(master)
f1.grid(row=3, column=1, sticky=W)
unit = StringVar(value="mi")
Radiobutton(f1, text="miles", value="mi", variable=unit, bg="light gray").pack(side="left")
Radiobutton(f1, text="km", value="km", variable=unit, bg="light gray").pack(side="left")
# creating a button using the widget   
b = Button(master, text="Calculate", command=get_dis, bg = "white") 
b.grid(row=1, column=2,columnspan=2, rowspan=2,padx=5, pady=5,) 

mainloop() 

因为我们已经用check按钮获得了其中一个单位的距离。。让我们使用它。我们将把它的结果转换为另一个结果。将Check更改为Distance in miles,为KM添加了一个新按钮。创建一个将英里数转换为公里数的函数。然后创建一个同时调用这两个函数的函数。Two。这将得到以英里为单位的距离(get_dis(,并转换为KM(km_mi(。结果将没有小数。我用的是int。如果你想要小数,你可以按照另一个答案中的建议使用round

from tkinter import *
from geopy.geocoders import Nominatim
from geopy import distance

# user defined function
def get_dis():
try:
global res
geolocator = Nominatim(user_agent="geoapiExercises")
place1 = geolocator.geocode(str(e1.get()))
place2 = geolocator.geocode(str(e2.get()))
Loc1_lat, Loc1_lon = (place1.latitude), (place1.longitude)
Loc2_lat, Loc2_lon = (place2.latitude), (place2.longitude)
location1 = (Loc1_lat, Loc1_lon)
location2 = (Loc2_lat, Loc2_lon)
res = int(distance.distance(location1, location2).mi)
return result.set(res)

except Exception as e:
print(e)
def km_mki():
miles = res
conv_fac = 1.609  # conversion factor
# calculating how many kilometers
kilometers = miles * conv_fac
return result.set(int(kilometers))
def two():
get_dis()
km_mki()

# object of tkinter
# with background set to light grey
master = Tk()
master.configure(bg='light grey')
master.title("Find Distance")
# Variable Classes in tkinter
result = StringVar()
# Creating label for each information
# name using widget Label
Label(master, text="Start Location : ", bg="light grey").grid(row=1, sticky=W)
Label(master, text="End Location : ", bg="light grey").grid(row=2, sticky=W)
Label(master, text="Result :", bg="light grey").grid(row=3, sticky=W)
# Creating label for class variable
# name using widget Entry
Label(master, text="", textvariable=result, bg="light grey").grid(row=3, column=1, sticky=W)
e1 = Entry(master, width=50)
e1.grid(row=1, column=1)
e2 = Entry(master, width=50)
e2.grid(row=2, column=1)
# creating a button using the widget
b = Button(master, text="Get distance in Miles", command=get_dis, bg="white")
b.grid(row=1, column=2, columnspan=2, rowspan=2, padx=5, pady=5, )
b1 = Button(master, text="Get distance in Km", command=two, bg="white")
b1.grid(row=3, column=2, columnspan=2, rowspan=3, padx=5, pady=5, )
mainloop()

最新更新