Application to get address details from zip code Using Python - GeeksforGeeks

20 Apr, 2022
Prerequisite: Tkinter
In this article, we are going to write scripts to get address details from a given Zip code or Pincode using tkinter and geopy module in python, Tkinter is the most commonly used GUI module in python to illustrate graphical objects and geopy is a python module to locate the coordinates of addresses, cities, countries, landmarks, and zip code.
Installation:
The tkinter module is an in-built module in Python, however, we need to install geopy module:
pip install geopy
Approach:
Import geopy module.Use Nominatim API to access the corresponding to a set of coordinates, nominatim uses OpenStreetMap data to find locations on Earth by name and address (geocoding).Use geocode() to get the location of given Zipcode and displaying it.
Import geopy module.
Use Nominatim API to access the corresponding to a set of coordinates, nominatim uses OpenStreetMap data to find locations on Earth by name and address (geocoding).
Use geocode() to get the location of given Zipcode and displaying it.
Below is the implementation of the above approach:
Python3
# Importing required modulefrom geopy.geocoders import Nominatim # Using Nominatim Apigeolocator = Nominatim(user_agent="geoapiExercises") # Zipcode inputzipcode = "800011" # Using geocode()location = geolocator.geocode(zipcode) # Displaying address detailsprint("Zipcode:",zipcode)print("Details of the Zipcode:")print(location)
Output:
Zipcode: 800011
Details of the Zipcode:
Danapur, Dinapur-Cum-Khagaul, Patna, Bihar, 800011, India
Below is a GUI implementation of the above program using tkinter module:
Python3
# Importing required modulesfrom geopy.geocoders import Nominatimfrom tkinter import *   # Function to get zipcode inputdef zip_code():    try:               geolocator = Nominatim(user_agent="geoapiExercises")        zipcode = str(e.get())        location = geolocator.geocode(zipcode)        res.set(location.address)         except:        location = "Oops! something went wrong"        res.set(location)           # Creating tkinter object# and background set for light greymaster = Tk()master.configure(bg='light grey')   # Variable Classes in tkinterres = StringVar();   # Creating label for each informationLabel(master, text="Zipcode: " , bg = "light grey").grid(row=0, sticky=W)Label(master, text="Details of the pincode:", bg = "light grey").grid(row=3, sticky=W)   # Creating label for class variableLabel(master, text="", textvariable=res,bg = "light grey").grid(row=3,column=1, sticky=W)   e = Entry(master)e.grid(row=0, column=1) # Creating a button using the widget # Button that will call the submit functionb = Button(master, text="Show", command=zip_code )b.grid(row=0, column=2,columnspan=2, rowspan=2,padx=5, pady=5) mainloop() # this code belongs to Satyam kumar (ksatyam858)
Output:
surinderdawra388
sweetyty
Python Tkinter-exercises
Python-tkinter
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Reading and Writing to text files in Python
*args and **kwargs in Python
Create a Pandas DataFrame from Lists
How To Convert Python Dictionary To JSON?