pyqt 事件更新图片显示_使用PyQTamp;树莓派制作一个天气站
吃灰的很久樹莓派 b 偶然翻出來 發(fā)揮下余熱,系統(tǒng)采用官方固件 Raspberry Pi OS(Raspbian) ,由于我的屏幕時(shí)由HDMI 驅(qū)動(dòng)所以無需特殊配置其他驅(qū)動(dòng)(我這裝了下賣家給的驅(qū)動(dòng)調(diào)整了下屏幕分辨率)
裝完系統(tǒng)直接安裝PyQt即可 sudo apt-get install python3-pyqt5
代碼比較簡(jiǎn)單 簡(jiǎn)單說一下,因?yàn)闀r(shí)間需要每秒刷新,所以需要用到多線程來防止卡死UI
我們可以重寫run方法來實(shí)現(xiàn)
class UpdateTime(QThread):signal = pyqtSignal(str,str,str) #括號(hào)里填寫信號(hào)傳遞的參數(shù)類型,調(diào)用TimeTick的函數(shù)要和這里對(duì)應(yīng)def __init__(self):super().__init__()def __del__(self):self.wait()def run(self):global picIntglobal timeUpdatewhile True:timeUpdate = datetime.datetime.now() #每秒獲取下時(shí)間strtime0 = timeUpdate.strftime('%S')strtime1 = timeUpdate.strftime('%H:%M:%S') #時(shí) 分 秒 strtime2 = timeUpdate.strftime('%w') #星期strtime3 = timeUpdate.strftime('%m') #月strtime4 = timeUpdate.strftime('%d') #日self.signal.emit(strtime1,todayWeek(strtime2),str(strtime3+"月"+strtime4+"日")) # 發(fā)射信號(hào)picInt = int(strtime0)#這里單獨(dú)存一個(gè)秒用來判斷換圖時(shí)間time.sleep(1)獲取天氣部分 API 處理,完整的json 格式說明詳見工程中的 jsonInfo/天氣模板.json
工程中還附有一個(gè)城市代碼,可以找到自己的城市替換api 尾部的代碼 http://t.weather.itboy.net/api/weather/city/101280701
#獲取天氣 def getTemp():try: # 連接超時(shí),6秒,下載文件超時(shí),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']) #更新時(shí)間]except:tempList = ["---"]*14return tempListelse:return tempList由于在PC 調(diào)試get_cpu_temp() 和get_gpu_temp() 兩個(gè)函數(shù)不能再pc 下使用先注釋掉了,再樹莓派中跑的時(shí)候記得把代碼還原
# cpuTemp = get_cpu_temp()
# gpuTemp = get_gpu_temp()
cpuTemp = 23
gpuTemp = 23
工程下載
ShaderFallback/Raspberry-Pi-PyQt?github.com字體文件來自 (字體有版權(quán)商用前請(qǐng)自理!)
三極纖云簡(jiǎn)體正版字體下載 正版字體版權(quán)購(gòu)買 - 正版中文字體版權(quán)購(gòu)買及下載盡在字體家?www.zitijia.com總結(jié)
以上是生活随笔為你收集整理的pyqt 事件更新图片显示_使用PyQTamp;树莓派制作一个天气站的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opengl游戏引擎源码_跨平台渲染引擎
- 下一篇: angular 字符串转换成数字_一文看