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

Need to create option for km or mi using Tkinter and Python

下面我找到并修改了一段代码,用于计算两个位置之间的距离。我正在为简单的用户输入创建一个 GUI。一切都按预期工作,但如果我能完成两件事,那就更好了。

  1. 有一个切换或选择可以让我以公里或米显示距离

  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()

用笔记编辑..无法舍入

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) .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"   

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)
Label(master, text="Distance :", bg = "light grey").grid(row=3, 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()

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

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位。

因为我们已经通过 check 按钮获得了其中一个单位的距离..让我们使用它。我们会将它的结果转换为另一个。将 Check 更改为 Distance in miles,为 KM 添加了一个新按钮。创建一个将英里转换为公里的函数。然后一个函数来调用这两个函数。Two。这将获得以英里为单位的距离(get_dis)并转换为公里(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()
# 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()