📜  使用Python从给定的州名或城市名中搜索国家名的 GUI 应用程序

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

使用Python从给定的州名或城市名中搜索国家名的 GUI 应用程序

在这些文章中,我们将编写Python脚本从给定的州或城市名称中搜索一个国家,并将其与 GUI 应用程序绑定。我们将使用 GeoPy 模块。 GeoPy 模块可以更轻松地定位地址、城市、国家、地标和邮政编码的坐标。

在开始之前,我们需要安装 GeoPy 模块,所以让我们在终端上运行这个命令。

pip install geopy

方法:

  • 导入模块
  • 使用 Nominatim API 访问对应的坐标集
  • geocode() 获取给定地点的位置

GUI 如下所示:

注意: Nominatim 使用 OpenStreetMap 数据按名称和地址(地理编码)查找地球上的位置。

下面是实现:

Python3
from geopy.geocoders import Nominatim
 
 
geolocator = Nominatim(user_agent = "geoapiExercises")
location = geolocator.geocode("Delhi")
print("Country Name: ", location)


Python3
# importing the modules
from geopy.geocoders import Nominatim
from tkinter import *
from tkinter import messagebox
 
def getinfo():
    geolocator = Nominatim(user_agent = "geoapiExercises")
    place = e.get()
    place_res.set(place)
    location = geolocator.geocode(place)
    res.set(location)
 
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg = 'light grey')
 
# variable Classes in tkinter
place_res = StringVar();
res = StringVar();
 
# creating label for each information
# name using widget Label
Label(master, text = "Enter place :" ,
      bg = "light grey").grid(row = 0, sticky = W)
Label(master, text = "Place :" ,
      bg = "light grey").grid(row = 1, sticky = W)
Label(master, text = "Country Address :" ,
      bg = "light grey").grid(row = 2, sticky = W)
 
# creating lebel for class variable
# name using widget Entry
Label(master, text = "", textvariable = place_res,
      bg = "light grey").grid(row = 1, column = 1, sticky = W)
Label(master, text = "", textvariable = res,
      bg = "light grey").grid(row = 2, 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 = getinfo )
b.grid(row = 0, column = 2, columnspan = 2,
       rowspan = 2, padx = 5, pady = 5)
 
mainloop()


输出:

使用 Tkinter 从给定城市/州搜索国家的应用程序:此脚本将上述实现实现到 GUI 中。

蟒蛇3

# importing the modules
from geopy.geocoders import Nominatim
from tkinter import *
from tkinter import messagebox
 
def getinfo():
    geolocator = Nominatim(user_agent = "geoapiExercises")
    place = e.get()
    place_res.set(place)
    location = geolocator.geocode(place)
    res.set(location)
 
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg = 'light grey')
 
# variable Classes in tkinter
place_res = StringVar();
res = StringVar();
 
# creating label for each information
# name using widget Label
Label(master, text = "Enter place :" ,
      bg = "light grey").grid(row = 0, sticky = W)
Label(master, text = "Place :" ,
      bg = "light grey").grid(row = 1, sticky = W)
Label(master, text = "Country Address :" ,
      bg = "light grey").grid(row = 2, sticky = W)
 
# creating lebel for class variable
# name using widget Entry
Label(master, text = "", textvariable = place_res,
      bg = "light grey").grid(row = 1, column = 1, sticky = W)
Label(master, text = "", textvariable = res,
      bg = "light grey").grid(row = 2, 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 = getinfo )
b.grid(row = 0, column = 2, columnspan = 2,
       rowspan = 2, padx = 5, pady = 5)
 
mainloop()


输出: