可能需要的环境:
需要下载的第三方库:requests 其余为python自带不需要下载
首先看下效果图
具体写作过程参考b站视频传送门:GUI天气预报
接下来实现过程,首先创建窗口,设置标题,布局窗口,设置标签,按钮,就直接粘贴代码:
如果对创建窗口的函数不熟悉:请点击python中tkinter的使用(控件整理)(一),python中tkinter的使用(相应事件整理)(二)
#创建窗口
window=Tk()
#设置标题
window.title("天气查询(可查县市省)")
#设置窗口大小,并布局接近中间的位置
window.geometry("900x640+540+300")
#设置标签
label=Label(window,text="请输入要查询城市的名字:",font=("微软雅黑",20),fg='blue')
#定位 grid网格布局
label.grid()
#添加输入框 并布局
entry=Entry(window,font=('微软雅黑',20),width=30,fg='blue')
entry.grid(row=0,column=1)
#列表控件
lis=Listbox(window,font=("微软雅黑",12),width=90,height=25)
lis.grid(row=1,columnspan=3)
#showbg()
#设置按钮
button=Button(window,text="查询",width=10,height=1,command=getWeather)
#设置按下按钮的背景颜色
button['activebackground']='green'
#设置按下按钮的字体颜色
button['activeforeground']='red'
#
button['cursor']='heart'
#定位 左对齐 sticky:对齐方式 N S W E
button.grid(row=2,column=0,sticky=W)
#command 点击触摸事件
Button(window,text="退出",width=10,height=1,command=window.quit).grid(row=2,column=1,sticky=E)
#显示窗口,消息循环
window.mainloop()
如果上面有什么不懂的,可以留言提问,或者自行百度。重点是按钮触发事件。尽可能的详细讲解
首先我们需要数据来源,来源是阿里云上面的免费天气查询的接口,要去申请接口。
传送门请点击:全国天气预报查询(免费版)
然后登录点击购买,支付0元,完成购买后,就可以按照上面的b站地址,进行操作了。
里面有python2的请求实例,我们需要按照上面视频的也要重新构造请求。由于视频请求的东西有限,我就自己改,把能拿的数据都拿了,多了一些东西,就说和视频里面不一样的吧。
我多爬了空气质量,风向,温度指数,运动指数,洗车指数,紫外线指数,穿衣指数.....就是这些指数看下面图
你会发现这是字典列表里面字典嵌套字典,所以得把列表里面的每个字典的字典里面Key对应的值,拿出来,所以for循环走一波,里面的值存列表,每三个值,就开始放进listbox里面。再用格式控制走一波。
提取嵌套字典:首先最外层字典中各种指数提取出来,infoa=info['index'],因为所有指数都放在idnex 为key对应的值里面在
infoa=info['index']
for item in infoa:
#提取字典里面的值
for v in item.items():
value.append(v[1])
每次提取出来index里面的一个字典,提取里面三个值就放进listbox里面,index每次多加1个是每个指数行之间插入一行空字符,方便查看,因为文字,太多了。
index+=1
lis.insert(index,"{}: {}".format(value[0],value[1]))
index+=1
lis.insert(index,"%s"%value[2])
index+=1
lis.insert(index," ")
value.clear()
里面的appcode就是你申请阿里云天气预报成功后你得到的code。我的就不贴出来了。下面全代码:供参考
#桌面
from tkinter import*
from tkinter import messagebox
from PIL import Image,ImageTk
import requests,urllib
def getWeather():
#获取用户输入的城市
city=entry.get()
if city=='':
messagebox.showinfo("提示","请输入要查询的城市(县市省)")
else:
#重构接口
#编码
city=urllib.request.quote(city)
host = 'https://jisutqybmf.market.alicloudapi.com'
path = '/weather/query'
method = 'GET'
#appcoe 申请阿里云的免费天气预报接口
#网址:https://market.aliyun.com/products/57126001/cmapi014302.html?spm=5176.2020520132.101.5.kHQHGQ#sku=yuncode830200000
appcode = '你申请阿里云天气成功后的code'
querys ='city='+city
url = host + path +'?'+querys
header={'Authorization':'APPCODE ' + appcode}
#把字符格式数据转换成字典格式
r=requests.get(url,headers=header).json()
info=r["result"]
infoa=info['index']
infob=info['aqi']
infobb=infob['aqiinfo']
#向列表控件添加元素
lis.delete(0,END)
lis.insert(0,"当前城市:{} 最新更新时间: {} {} ".format(info["city"],info['updatetime'],info['week']))
lis.insert(1,"天气: %s"%info["weather"])
lis.insert(2,"温度: {}° 温度范围:{}°~{}°".format(info['temp'],info['templow'],info['temphigh']))
lis.insert(3,"空气质量: {} {} ".format(infob['quality'],infobb['affect']))
lis.insert(4,"建议:{}".format(infobb['measure']))
lis.insert(5,"风向: {} 风级: {}".format(info['winddirect'],info['windpower']))
lis.insert(6,"各项指数:")
value=[];index=6
for item in infoa:
#提取字典里面的值
for v in item.items():
value.append(v[1])
index+=1
lis.insert(index,"{}: {}".format(value[0],value[1]))
index+=1
lis.insert(index,"%s"%value[2])
index+=1
lis.insert(index," ")
value.clear()
def showbg():
#显示图片
bm=ImageTk.PhotoImage(file="weather.jpg")
label1=Label(window,image=bm)
#背景图片
label1.bm=bm
label1.grid(row=1,columnspan=3)
#创建窗口
window=Tk()
#设置标题
window.title("天气查询(可查县市省)")
#设置窗口图标
window.iconbitmap('cloudy2.ico')
#设置窗口大小,并布局接近中间的位置
window.geometry("900x640+540+300")
#设置标签
label=Label(window,text="请输入要查询城市的名字:",font=("微软雅黑",20),fg='blue')
#定位 grid网格布局
label.grid()
#添加输入框 并布局
entry=Entry(window,font=('微软雅黑',20),width=30,fg='blue')
entry.grid(row=0,column=1)
#列表控件
lis=Listbox(window,font=("微软雅黑",12),width=90,height=25)
lis.grid(row=1,columnspan=3)
#showbg()
#设置按钮
button=Button(window,text="查询",width=10,height=1,command=getWeather)
#设置按下按钮的背景颜色
button['activebackground']='green'
#设置按下按钮的字体颜色
button['activeforeground']='red'
#
button['cursor']='heart'
#定位 左对齐 sticky:对齐方式 N S W E
button.grid(row=2,column=0,sticky=W)
#command 点击触摸事件
Button(window,text="退出",width=10,height=1,command=window.quit).grid(row=2,column=1,sticky=E)
#显示窗口,消息循环
window.mainloop()