声明:文章为自己笔记,目的仅作学习与回顾,如有侵权请文后留言,会尽快删除
参考自链接:https://blog.csdn.net/zc666ying/article/details/105945712 CSDN链接
代码
import json
import requests
import PySimpleGUI as sg
def get_weather(city,key):
r = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=' + city)
text = json.loads(r.text)
dict = text['data']['forecast'][0]
result = dict[str(key)]
return result
if __name__ == '__main__':
sg.SetOptions(text_justification='center')
layout = [
[sg.Text('城市', size=(20, 1)), sg.Input(key='CITY')],
[sg.Text('日期', size=(20, 1)), sg.Input(key='DATE')],
[sg.Text('最高气温', size=(20, 1)), sg.Input(key='HIGH')],
[sg.Text('风力', size=(20, 1)), sg.Input(key='FENGLI')],
[sg.Text('最低气温', size=(20, 1)), sg.Input(key='LOW')],
[sg.Text('风向', size=(20, 1)), sg.Input(key='FENGXIANG')],
[sg.Text('天气', size=(20, 1)), sg.Input(key='TYPE')],
[sg.Button('搜索')],
[sg.Button('Exit', size=(10, 1))]
]
window = sg.Window('天气小助手', layout, location=(450, 200))
while True:
event, values = window.read()
if event == 'Exit' or event is None:
break
city = values['CITY']
date = get_weather(city,'date')
high = get_weather(city,'high')
fengli = get_weather(city,'fengli')
low = get_weather(city,'low')
fengxiang = get_weather(city,'fengxiang')
type = get_weather(city,'type')
weather_date = window['DATE'].update(date)
weather_high = window['HIGH'].update(high)
weather_fengli = window['FENGLI'].update(fengli)
weather_low = window['LOW'].update(low)
weather_fengxiang = window['FENGXIANG'].update(fengxiang)
weather_type = window['TYPE'].update(type)
window.read()
window.close()
效果图
PS:在图中城市一栏输入所要查询的城市,联网状态下即可访问互联网并在查询后返回结果。