python制作属于自己的天气预报3

这次根据书里的内容增加了一个GUI的页面。同样只做了一个简单的页面,几乎没有排版,数据也没有完全拉出来。

还有遇到了一个大的问题python中text与string的属性有区别。我要是在显示的地方加上制表符或者空格什么的,立马就给我多出来一左一右的{}。这个问题不知道怎么解决,暂时绕过。有高手可以教我解决一下。

 

#测试用压缩的数据
import re
from typing import List
from urllib.request import Request, urlopen
import gzip,zlib
import json,tkinter

#网站获取数据Api
url = 'http://t.weather.sojson.com/api/weather/city/101250101'
#包装头部
firefox_headers = {'User-Agent': "	Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0 " , "Accept-Encoding" :"gzip, deflate"}

#构建请求
request = Request( url, headers=firefox_headers )
html = urlopen( request )



#获取数据以utf-8的编码方式
#data2 = html.read().decode('utf-8')
data2 = zlib.decompress(html.read(), 16+zlib.MAX_WBITS).decode('utf-8')
data1=str(data2)
# 将json数据转换为dict数据
weather_dict=json.loads(data2)
#data3 =dict(html.read().decode('utf-8'))
#data3 =dict (str(data2))

#print(weather_dict)
forecast = weather_dict.get('data').get('forecast')
'''
print('城市:', weather_dict.get('cityInfo').get('parent')+' '+weather_dict.get('cityInfo').get('city'))
print('更新时间:', weather_dict.get('cityInfo').get('updateTime'))
print('温度:', weather_dict.get('data').get('wendu') + '℃ ')
print('空气质量:', weather_dict.get('data').get('quality'))
print('风向:', forecast[0].get('fx'))
print('风向:', weather_dict.get('data').get('forecast')[0].get('fx'))
'''

top=tkinter.Tk()

label1=tkinter.Label(top,text=('城市:', weather_dict.get('cityInfo').get('parent'),weather_dict.get('cityInfo').get('city')))
label2=tkinter.Label(top,text=('今日天气:温度',weather_dict.get('data').get('wendu'),'空气质量',weather_dict.get('data').get('quality'),'湿度',weather_dict.get('data').get('shidu')))
label3=tkinter.Label(top,text=('pm25',weather_dict.get('data').get('pm25'),'pm10',weather_dict.get('data').get('pm10'),'感冒指数',weather_dict.get('data').get('ganmao')))

label1.grid(row=0,column=0,padx=50,pady=10)
label2.grid(row=1,column=0,padx=50,pady=10)
label3.grid(row=2,column=0,padx=50,pady=10)

top.mainloop()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值