tkinter+高德api服务 实现天气查询
时间:2021-05-24 01:44:32
收藏:0
阅读:0
1 # -*- coding:utf-8 -*- 2 #weather_api 3 import requests 4 import json 5 ‘‘‘ 6 东城区:110101 7 西城区:110102 8 朝阳区:110105 9 丰台区:110106 10 ‘‘‘ 11 12 def Weather_info(input_city): 13 # city = ‘‘ 14 weatherinfo_search = ‘https://restapi.amap.com/v3/weather/weatherInfo?parameters‘ 15 # if input_city == "东城区": 16 # city.join("110101") 17 # elif input_city == "西城区": 18 # city.join("110102") 19 # elif input_city == "朝阳区": 20 # city.join("110105") 21 # elif input_city == "丰台区": 22 # city.join("110106") 23 # else: 24 # print("城市未知") 25 params = {‘key‘:‘cd1b11e803196aa2a3062f25‘, 26 ‘city‘:input_city, 27 ‘extensions‘:‘base‘, 28 ‘output‘:‘JSON‘} 29 result = requests.get(url=weatherinfo_search,params=params) 30 # print(result.status_code) 31 # print(result.json()) 32 # print(result.text) 33 # print(result.text[0]) 34 # print(json.dumps(result.json(),sort_keys=True,indent=4,ensure_ascii=False)) 35 lives = (result.json()["lives"]) 36 for lives_obj in lives: 37 weather_result = lives_obj["weather"] 38 weather_city = lives_obj["city"] 39 print("城市:", weather_city) 40 print("天气:",weather_result) 41 42 return weather_result,weather_city
1 # -*- coding:utf-8 -*- 2 from tkinter import * 3 import Weather_API 4 5 6 # 实例化object,建立窗口window 7 window = Tk() 8 # 给窗口的可视化起名字 9 window.title("天气查询") 10 # 定窗口的大小(长 * 宽) 11 window.geometry(‘500x300‘) # 这里的乘是小x 12 entry = Entry(window) 13 entry.pack() 14 15 def search_weather(): 16 input_city = entry.get() 17 print(input_city) 18 Weather_API.Weather_info(input_city=input_city) 19 Search_button = Button(window, text=‘查询‘, font=(‘Arial‘, 12), width=10, height=1, command=search_weather) 20 21 Search_button.pack() 22 23 # 第6步,主窗口循环显示 24 window.mainloop() 25 # 注意,loop因为是循环
评论(0)