📜  使用Python从邮政编码中获取地址详细信息的应用程序

📅  最后修改于: 2022-05-13 01:55:04.809000             🧑  作者: Mango

使用Python从邮政编码中获取地址详细信息的应用程序

先决条件: Tkinter

在本文中,我们将编写脚本以使用tkinter从给定的邮政编码或 Pincode 获取地址详细信息 和Python中的geopy模块,Tkinter是Python中最常用的GUI模块,用于说明图形对象, geopy是Python模块,用于定位地址、城市、国家、地标和邮政编码的坐标。

安装:

tkinter模块是Python的内置模块,但是,我们需要安装geopy模块:

pip install geopy

方法:

  1. 导入geopy模块。
  2. 使用Nominatim API 访问对应的一组坐标,n ominatim使用 OpenStreetMap 数据通过名称和地址(地理编码)查找地球上的位置。
  3. 使用geocode()获取给定邮政编码的位置并显示它

下面是上述方法的实现:

Python3
# Importing required module
from geopy.geocoders import Nominatim
  
# Using Nominatim Api
geolocator = Nominatim(user_agent="geoapiExercises")
  
# Zipocde input
zipcode = "800011"
  
# Using geocode()
location = geolocator.geocode(zipcode)
  
# Displaying address details
print("Zipcode:",zipcode)
print("Details of the Zipcode:")
print(location)


Python3
# Importing required modules
from geopy.geocoders import Nominatim
from tkinter import *
  
  
  
# Funtion to get zipcode input
def 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 grey
master = Tk()
master.configure(bg='light grey')
  
  
  
# Variable Classes in tkinter
res = StringVar();
  
  
  
# Creating label for each information 
Label(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 variable
Label(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 function 
b = 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)


输出:

Zipcode: 800011
Details of the Zipcode:
Danapur, Dinapur-Cum-Khagaul, Patna, Bihar, 800011, India

下面是使用tkinter模块的上述程序的 GUI 实现

蟒蛇3

# Importing required modules
from geopy.geocoders import Nominatim
from tkinter import *
  
  
  
# Funtion to get zipcode input
def 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 grey
master = Tk()
master.configure(bg='light grey')
  
  
  
# Variable Classes in tkinter
res = StringVar();
  
  
  
# Creating label for each information 
Label(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 variable
Label(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 function 
b = 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)

输出: