python 天气预报地图_在树莓派上用 python 做一个炫酷的天气预报

#encoding: utf-8

importpygameimporttimeimportweatherAPIimportSystemInfofrom datetime importdatetime#显示图片函数

defShowPicture(picturepath,x0,y0):

background=pygame.image.load(picturepath)

background.convert_alpha()

window.blit(background,(x0,y0))return

defShowCircle():

pygame.draw.circle(window,pygame.Color(255,255,255),(width/2,height/2),radius,fill)return

#划线函数,起始坐标,终点坐标

defShowLine(x0,y0,x1,y1):

pygame.draw.line(window,pygame.Color(255,255,255),(x0,y0),(x1,y1),fill)returnYellow=(255,255,0)

Red=(255,0,0)

LightBlue=(190,190,255)

Green=(0,255,0)

Black=(0,0,0)

White=(255,255,255)#画框函数

defShowRec(x0,y0,x1,y1,color,fill):

pygame.draw.rect(window,color,(x0,y0,x1,y1),fill)return

#字符串显示函数

defShowStr(mystring,x0,y0,size):

font=pygame.font.Font('gkai00mp.ttf',size,bold=1)

textSuface=font.render(mystring,1,pygame.Color(255,255,255))

window.blit(textSuface,(x0,y0))return

defShowStr2(mystring,x0,y0,size):

font=pygame.font.Font('gkai00mp.ttf',size,bold=1)

textSuface=font.render(mystring,1,pygame.Color(255,255,0))

window.blit(textSuface,(x0,y0))return

defShowStr3(mystring,x0,y0,size):

font=pygame.font.Font('gkai00mp.ttf',size,bold=1)

textSuface=font.render(mystring,1,pygame.Color(0,255,0))

window.blit(textSuface,(x0,y0))return

#背景参数设置

width=1280height=800fill=1

#初始化背景

pygame.init()

window=pygame.display.set_mode((width,height),pygame.FULLSCREEN)#全屏#window=pygame.display.set_mode((width,height))#不全屏

window.fill(pygame.Color(0,0,0))#back=pygame.image.load(r"/home/pi/ccj/c.jpg") #图片位置

loop=0

last_ip= ip = ''updatingtime=""Title_X=width

WeatherValidation=FalsewhileTrue:#window.blit(back,(0,0)) #对齐的坐标

window.fill(pygame.Color(0,0,0)) #背景色0为黑

#ShowPicture("a_3.gif",20,450)

#draw grids

#ShowStr(u"时间",10,20,80)

ShowRec(10,10,width-20,height-80,White,1) #画一个大框

ShowLine(10,height/5,width-10,height/5)

ShowLine(10,height/5*3,width-10,height/5*3)

ShowLine(width/2,height/5,width/2,height-70)

ShowLine(width/4,height/5*3,width/4,height-70)

ShowLine(width/4*3,height/5*3,width/4*3,height-70)#time show

mylocaltime=time.localtime()

myclock=time.strftime("%H:%M:%S",mylocaltime)#13:15:03 2017-04-21

ShowStr(myclock,0,0,180)

mydate=time.strftime("%Y-%m-%d",mylocaltime)#2017-04-21

ShowStr(mydate,810,5,90)

mytime=time.strftime("%A",mylocaltime)#Thursday

ShowStr(mytime,830,90,85)

name= "自动化实验室欢迎您"ShowStr2(name,width/2+10,height/5*2-140,52)

ip= SystemInfo.get_ip('wlan0')#ip = SystemInfo.get_ip('eth0')

cpu_usage =SystemInfo.getCPUuse()

ShowStr(ip,width/2+100,height/5*2-90,48)

ShowStr("ip:",width/2+10,height/5*2-90,52)#netspeed show

NetInfoOld=SystemInfo.net_stat()

time.sleep(1)

NetInfoNew=SystemInfo.net_stat()

DownloadSpeed=(NetInfoNew[0]["ReceiveBytes"]-NetInfoOld[0]["ReceiveBytes"])/1048576 #last second total flow -current second total flow

UploadSpeed=(NetInfoNew[0]["TransmitBytes"]-NetInfoOld[0]["TransmitBytes"])/1048576ShowRec(width/2+20,height/5*2-40,DownloadSpeed/10*600+20,48,Green,0)

ShowRec(width/2+20,height/5*2+10,UploadSpeed/10*600+20,48,LightBlue,0)

ShowStr("↓:"+str("%3.2f"%(DownloadSpeed))+"MB/s",width/2+20,height/5*2-40,48)

ShowStr("↑:"+str("%3.2f"%(UploadSpeed))+"MB/s",width/2+20,height/5*2+10,48)#cpu_usage show

ShowRec(width/2+20,height/5*2+60,cpu_usage/100*600+60,48,Yellow,0)

ShowStr("CPU usage:"+str("%2d"%cpu_usage)+"%",width/2+20,height/5*2+110,48)if loop % 60==0 :

future= datetime.strptime('2019-1-1 00:00:00','%Y-%m-%d %H:%M:%S')#当前时间

now =datetime.now()#求时间差

delta = future -now

hour= delta.seconds/60/60minute= delta.seconds/60seconds= delta.seconds - hour*60*60 - minute*60

#print_now=now.strftime('%Y-%m-%d %H:%M:%S')

#print("今天是:",print_now)

#print("距离 2019-02-01 \"work\" 还剩下:%d天"%delta.days)

#print(delta.days,hour, minute, seconds)

ShowStr2("倒计时:%dH (%dMin)"%(hour,minute),width/4*2+width/32+20,height/5*3+height/30+235,45)##########################本地信息获取完成#######################################

#print ("↓:"+str("%3.1f"%(DownloadSpeed))+"MB/s")

#print ("↑:"+str("%3.1f"%(UploadSpeed))+"MB/s")

#print("CPU usage:"+str("%2d"%cpu_usage)+"%")

#########weather show####################################

if loop % 10800==0 : #update per 3 hours

jsonArr=weatherAPI.GetWeatherInfo()if jsonArr!=None : #记录请求数据时间

updatingtime=time.strftime("%H:%M:%S",mylocaltime)if jsonArr["status"]!="1":print (jsonArr["msg"])

WeatherValidation=Falseelse:

result=jsonArr["forecasts"][0]

WeatherValidation=True#print (result["city"],result["weather"],result["temp"],result["temphigh"],result["templow"])

if WeatherValidation==True:#AQI=result["aqi"]

#index=result["index"]

#index0=index[0]

#daily=result["daily"]

#day1=daily[1]#明天天气预报

#day2=daily[2]#明天天气预报

#day3=daily[3]#明天天气预报

#day4=daily[4]#明天天气预报

### #室外温湿度

#ShowPicture("pictures/"+result["img"]+".png",width/16,height/5+150)

ShowStr("武汉市",width/32,height/5+10,60)

ShowStr(result["city"],width/32,height/5+80,60)

ShowStr(result["casts"][0]["dayweather"],width/32-25,height/5*2+50,120)

ShowStr(result["casts"][0]["daytemp"]+"℃",width/4,height/5,160)

ShowStr("气温最低:"+result["casts"][0]["nighttemp"] +"℃",width/4-10,height/5*2-20,48)

ShowStr("接下来转:"+result["casts"][0]["nightweather"],width/4-10,height/5*2+50,48)#ShowStr("zhesgii",width/2+20,height/5+10,120)

ShowStr("风力:"+result["casts"][0]["daypower"]+"级",width/4-10,height/5*2+110,48)### #空气质量

#ShowStr("PM2.5:",width/2+280,height/5+120,32)

#ShowStr(AQI["pm2_5"],width/2+400,height/5-20,200)

#ShowStr("空气质量:"+AQI["quality"],width/2+240,height/5*2-40,32)

ShowPicture("pictures/"+result["casts"][0]["dayweather"]+".png",width/32+60,height/5+145)#if Title_X<=-100:

#Title_X=width

#else:

#Title_X=Title_X-40

#ShowStr(index0["detail"],Title_X,height-50,40)

##未来几天天气预报

ShowStr("明天:"+result["casts"][1]["date"],width/32,height/5*3+height/30-10,30)

ShowStr(result["casts"][1]["dayweather"],width/32,height/5*3+height/30+30,50)

ShowStr(result["casts"][2]["daytemp"]+"℃",width/32,height/5*3+height/30+80,70)

ShowStr("气温Min:"+result["casts"][1]["nighttemp"] +"℃",width/32-10,height/5*3+height/30+140,45)

ShowStr("未来转:"+result["casts"][1]["nightweather"],width/32-10,height/5*3+height/30+180,45)

ShowPicture("pictures/"+result["casts"][1]["dayweather"]+".png",width/32+170,height/5*3+height/30+45)#ShowPicture("pictures/"+day1["day"]["img"]+".png",width/32,height/5*3+height/10)

###

ShowStr("后天:"+result["casts"][2]["date"],width/4+width/32,height/5*3+height/30-10,30)

ShowStr(result["casts"][2]["dayweather"],width/4+width/32,height/5*3+height/30+30,50)

ShowStr(result["casts"][2]["daytemp"]+"℃",width/4+width/32,height/5*3+height/30+80,70)

ShowStr("气温Min:"+result["casts"][2]["nighttemp"] +"℃",width/4+width/32-10,height/5*3+height/30+140,45)

ShowStr("未来转:"+result["casts"][2]["nightweather"],width/4+width/32-10,height/5*3+height/30+180,45)

ShowPicture("pictures/"+result["casts"][2]["dayweather"]+".png",width/4+width/32+170,height/5*3+height/30+45)#ShowStr(day2["day"]["weather"],width/4+width/32,height/5*3+height/5-40,100)

#ShowStr(day2["day"]["windpower"],width/4+width/32+70,height/5*3+height/10,64)

#ShowStr(day2["night"]["templow"]+"~"+day2["day"]["temphigh"]+"℃",width/4+width/32,height-130,64)

#ShowPicture("pictures/"+day2["day"]["img"]+".png",width/4+width/32,height/5*3+height/10)

###

ShowStr("大后天:"+result["casts"][3]["date"],width/4*2+width/32-25,height/5*3+height/30-10,30)

ShowStr(result["casts"][3]["dayweather"],width/4*2+width/32-25,height/5*3+height/30+30,50)

ShowStr(result["casts"][3]["daytemp"]+"℃",width/4*2+width/32-25,height/5*3+height/30+80,70)

ShowStr("气温Min:"+result["casts"][3]["nighttemp"] +"℃",width/4*2+width/32-25,height/5*3+height/30+140,45)

ShowStr("未来转:"+result["casts"][3]["nightweather"],width/4*2+width/32-25,height/5*3+height/30+180,45)

ShowPicture("pictures/"+result["casts"][3]["dayweather"]+".png",width/4*2+width/32-25+170,height/5*3+height/30+45)#ShowStr(day3["day"]["weather"],width/4*2+width/32,height/5*3+height/5-40,100)

#ShowStr(day3["day"]["windpower"],width/4*2+width/32+70,height/5*3+height/10,64)

#ShowStr(day3["night"]["templow"]+"~"+day2["day"]["temphigh"]+"℃",width/4*2+width/32,height-130,64)

#ShowPicture("pictures/"+day3["day"]["img"]+".png",width/4*2+width/32,height/5*3+height/10)

###

ShowPicture("pictures/cj.png",width/4*3+width/32,height/5*3+height/30-15)#ShowStr(day4["day"]["weather"],width/4*3+width/32,height/5*3+height/5-40,100)

#ShowStr(day4["day"]["windpower"],width/4*3+width/32+70,height/5*3+height/10,64)

#ShowStr(day4["night"]["templow"]+"~"+day2["day"]["temphigh"]+"℃",width/4*3+width/32,height-130,64)

#ShowPicture("pictures/"+day4["day"]["img"]+".png",width/4*3+width/32,height/5*3+height/10)

##记录请求数据时间

ShowStr3("Last update:"+updatingtime,width/4*3+15,height/5*3,30)

ShowStr2("这里是滚动字幕显示区,加循环可实现动态效果",width/32-25,height/5*3+height/30+235,45)#update

pygame.display.update()

loop+=1

#全屏

#for event in pygame.event.get():

#if event.type==pygame.KEYDOWN:

#running=False

pygame.quit()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些参考代码和思路。 首先,您可以使用Python中的requests库获取天气预报的数据。根据您所在的地理位置和天气数据源,您可能需要使用不同的API接口,例如OpenWeatherMap、中国天气网等。在获取到天气数据之后,您可以使用Python中的json库或xml库来解析数据。 接着,您可以使用Python中的pygame库来播放语音。将天气预报转换为语音文件的方法有很多种,例如使用百度AI开放平台的语音合成API、使用Google Text-to-Speech API等。其中,百度AI开放平台的语音合成API使用起来比较方便,具体使用方法可以参考官方文档。 最后,您可以将获取到的天气数据和语音播放代码结合起来,实现天气预报语音播报的功能。 下面是一个简单的示例代码,供您参考: ```python import requests import json import pygame from aip import AipSpeech # 获取天气预报数据 weather_api_url = 'http://api.openweathermap.org/data/2.5/weather?q=city_name&appid=api_key' city_name = 'Shanghai' # 城市名称 api_key = 'your_api_key' # API密钥 url = weather_api_url.replace('city_name', city_name).replace('api_key', api_key) response = requests.get(url) weather_data = json.loads(response.text) # 解析天气预报数据 temperature = weather_data['main']['temp'] - 273.15 # 温度(单位:摄氏度) description = weather_data['weather'][0]['description'] # 天气描述 # 将天气预报转换为语音 APP_ID = 'your_app_id' # APP ID API_KEY = 'your_api_key' # API密钥 SECRET_KEY = 'your_secret_key' # 密钥 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) result = client.synthesis('今天的天气是' + description + ',温度是' + str(round(temperature, 1)) + '摄氏度。', 'zh', 1, {'vol': 5}) if not isinstance(result, dict): with open('weather.mp3', 'wb') as f: f.write(result) # 播放语音 pygame.mixer.init() pygame.mixer.music.load('weather.mp3') pygame.mixer.music.play() while pygame.mixer.music.get_busy() == True: continue ``` 需要注意的是,该示例代码使用了百度AI开放平台的语音合成API,您需要在使用之前去官网注册账号并申请API密钥。另外,该示例代码使用了pygame库来播放语音,如果您使用的是其他的播放音频的库,则可能需要进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值