![296b986984290a3cd2d5a76e9f2c76ec.png](https://i-blog.csdnimg.cn/blog_migrate/b74cfd68d4f42f85a36da65bc2e317b0.jpeg)
吃灰的很久树莓派 b 偶然翻出来 发挥下余热,系统采用官方固件 Raspberry Pi OS(Raspbian) ,由于我的屏幕时由HDMI 驱动所以无需特殊配置其他驱动(我这装了下卖家给的驱动调整了下屏幕分辨率)
装完系统直接安装PyQt即可 sudo apt-get install python3-pyqt5
代码比较简单 简单说一下,因为时间需要每秒刷新,所以需要用到多线程来防止卡死UI
我们可以重写run方法来实现
class UpdateTime(QThread):
signal = pyqtSignal(str,str,str) #括号里填写信号传递的参数类型,调用TimeTick的函数要和这里对应
def __init__(self):
super().__init__()
def __del__(self):
self.wait()
def run(self):
global picInt
global timeUpdate
while True:
timeUpdate = datetime.datetime.now() #每秒获取下时间
strtime0 = timeUpdate.strftime('%S')
strtime1 = timeUpdate.strftime('%H:%M:%S') #时 分 秒
strtime2 = timeUpdate.strftime('%w') #星期
strtime3 = timeUpdate.strftime('%m') #月
strtime4 = timeUpdate.strftime('%d') #日
self.signal.emit(strtime1,todayWeek(strtime2),str(strtime3+"月"+strtime4+"日")) # 发射信号
picInt = int(strtime0)#这里单独存一个秒用来判断换图时间
time.sleep(1)
获取天气部分 API 处理,完整的json 格式说明详见工程中的 jsonInfo/天气模板.json
工程中还附有一个城市代码,可以找到自己的城市替换api 尾部的代码 http://t.weather.itboy.net/api/weather/city/101280701
#获取天气
def getTemp():
try: # 连接超时,6秒,下载文件超时,7秒
r = requests.get('http://t.weather.itboy.net/api/weather/city/101280701',timeout=(6,7))
r.encoding = 'utf-8'
tempList = [
(r.json()['cityInfo']['city']), #城市
(r.json()['data']['forecast'][0]['low']), #今日低温
(r.json()['data']['forecast'][0]['high']), #今日高温
(r.json()['data']['forecast'][0]['type']), #今日天气
(r.json()['data']['forecast'][1]['low']), #明日低温
(r.json()['data']['forecast'][1]['high']), #明日高温
(r.json()['data']['forecast'][1]['type']), #明日天气
(r.json()['data']['forecast'][2]['low']), #后日低温
(r.json()['data']['forecast'][2]['high']), #后日高温
(r.json()['data']['forecast'][2]['type']), #后日天气
(r.json()['data']['forecast'][3]['low']), #大后日低温
(r.json()['data']['forecast'][3]['high']), #大后日高温
(r.json()['data']['forecast'][3]['type']), #大后日天气
(r.json()['cityInfo']['updateTime']) #更新时间
]
except:
tempList = ["---"]*14
return tempList
else:
return tempList
由于在PC 调试get_cpu_temp() 和get_gpu_temp() 两个函数不能再pc 下使用先注释掉了,再树莓派中跑的时候记得把代码还原
# cpuTemp = get_cpu_temp()
# gpuTemp = get_gpu_temp()
cpuTemp = 23
gpuTemp = 23
工程下载
ShaderFallback/Raspberry-Pi-PyQtgithub.com![8c8180ef1c0f99702421294bbba46035.png](https://i-blog.csdnimg.cn/blog_migrate/c189618102d9f79ed2867439d8b240d9.png)
字体文件来自 (字体有版权商用前请自理!)
三极纤云简体正版字体下载 正版字体版权购买 - 正版中文字体版权购买及下载尽在字体家www.zitijia.com![aedc3538477c1b462676731e3699f443.png](https://i-blog.csdnimg.cn/blog_migrate/ae10d10741f9aa769af5c33966cd3d38.png)
![3d333113c222417c83f70185bed3911f.png](https://i-blog.csdnimg.cn/blog_migrate/8bdd518314edcc60a35d957a53d54fc3.jpeg)
![53b013f255be6deb9a8a19791091fe03.png](https://i-blog.csdnimg.cn/blog_migrate/2b249a237d1669c99595b683004f4b95.jpeg)