|
| 1 | +from tkinter import * |
| 2 | +from geopy.geocoders import Nominatim |
| 3 | +from geopy import distance |
| 4 | + |
| 5 | +# user defined funtion |
| 6 | +def get_dis(): |
| 7 | + try: |
| 8 | + |
| 9 | + geolocator = Nominatim(user_agent="geoapiExercises") |
| 10 | + |
| 11 | + place1 = geolocator.geocode(str(e1.get())) |
| 12 | + place2 = geolocator.geocode(str(e2.get())) |
| 13 | + |
| 14 | + |
| 15 | + Loc1_lat,Loc1_lon = (place1.latitude),(place1.longitude) |
| 16 | + Loc2_lat,Loc2_lon = (place2.latitude),(place2.longitude) |
| 17 | + |
| 18 | + location1=(Loc1_lat,Loc1_lon) |
| 19 | + location2=(Loc2_lat,Loc2_lon) |
| 20 | + |
| 21 | + res = (str(distance.distance(location1, location2).km)+" Km") |
| 22 | + |
| 23 | + result.set(res) |
| 24 | + except: |
| 25 | + result.set("someting went wrong") |
| 26 | + |
| 27 | +# object of tkinter |
| 28 | +# with background set to light grey |
| 29 | +master = Tk() |
| 30 | +master.configure(bg='light grey') |
| 31 | +master.title("Distance Calculating App") |
| 32 | + |
| 33 | +# Variable Classes in tkinter |
| 34 | +result = StringVar() |
| 35 | + |
| 36 | + |
| 37 | +# Creating label for each information |
| 38 | +# name using widget Label |
| 39 | +Label(master, text="Enter first place : " , bg = "light grey").grid(row=1, sticky=W) |
| 40 | +Label(master, text="Enter secound place : " , bg = "light grey").grid(row=2, sticky=W) |
| 41 | + |
| 42 | +Label(master, text="Result :", bg = "light grey").grid(row=3, sticky=W) |
| 43 | + |
| 44 | +# Creating label for class variable |
| 45 | +# name using widget Entry |
| 46 | +Label(master, text="", textvariable=result,bg = "light grey").grid(row=3,column=1, sticky=W) |
| 47 | + |
| 48 | + |
| 49 | +e1 = Entry(master,width = 50) |
| 50 | +e1.grid(row=1, column=1) |
| 51 | +e2 = Entry(master,width = 50) |
| 52 | +e2.grid(row=2, column=1) |
| 53 | + |
| 54 | +# creating a button using the widget |
| 55 | +b = Button(master, text="Check", command=get_dis, bg = "white") |
| 56 | +b.grid(row=1, column=2,columnspan=2, rowspan=2,padx=5, pady=5,) |
| 57 | + |
| 58 | +mainloop() |
0 commit comments