好友
阅读权限10
听众
最后登录1970-1-1
|
Mrhe
发表于 2023-7-6 09:18
本帖最后由 Mrhe 于 2023-7-6 09:21 编辑
import tkinter as tk
import requests
def search_location(api_key, name):
url = 'https://restapi.amap.com/v3/place/text'
parameters = {
'key': api_key,
'keywords': name,
'city': '盐城',
'citylimit': 'true'
}
response = requests.get(url, params=parameters)
data = response.json()
if data['status'] == '1' and int(data['count']) >= 1:
pois = data['pois']
locations = [(poi['name'], poi['location']) for poi in pois]
return locations
else:
return None
def search_and_display():
api_key = api_key_entry.get()
name = entry.get()
locations = search_location(api_key, name)
if locations:
result_text.delete(1.0, tk.END)
for location in locations:
result_text.insert(tk.END, f"{location[0]}: {location[1]}\n")
else:
result_text.delete(1.0, tk.END)
result_text.insert(tk.END, "未找到符合条件的地点。")
# 创建主窗口
window = tk.Tk()
window.title('地点搜索和显示')
# 创建API密钥标签和文本输入框
api_key_label = tk.Label(window, text='请输入API密钥:')
api_key_label.pack()
api_key_entry = tk.Entry(window)
api_key_entry.pack()
# 创建地点搜索标签和文本输入框
label = tk.Label(window, text='请输入要搜索的地点名称:')
label.pack()
entry = tk.Entry(window)
entry.pack()
# 创建搜索按钮
button = tk.Button(window, text='搜索并显示', command=search_and_display)
button.pack()
# 创建结果显示文本框
result_label = tk.Label(window, text='搜索结果:')
result_label.pack()
result_text = tk.Text(window, height=10, width=50)
result_text.pack()
# 进入主循环
window.mainloop()
|
免费评分
-
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|