柚子快報(bào)邀請(qǐng)碼778899分享:pyqt與echart交互
柚子快報(bào)邀請(qǐng)碼778899分享:pyqt與echart交互
由于echart的文件渲染的原因,圖像載入需要一定的時(shí)間 第一步:定義繪圖模板,保存為test.html
第二步:在布局中添加QWebEngineView 需要額外安裝pyqt的拓展包pip install PyQtWebEngine
from PyQt5.QtWebEngineWidgets import QWebEngineView
def pic_echart(self):
#清空?qǐng)D像,防止后續(xù)在布局中反復(fù)添加繪圖
if self.verticalLayout_11.count()>0:
self.verticalLayout_11.removeItem(self.verticalLayout_11.itemAt(0))
self.statusBar().showMessage("圖像加載中...")
#實(shí)例化類用于加載html
radar_view = QWebEngineView()
radar_view.load(QUrl("file:///"+"html/radar.html"))
self.verticalLayout_11.addWidget(radar_view)
self.statusBar().showMessage("圖像加載完畢")
第三步:編寫js語句,動(dòng)態(tài)控制圖像加載 編寫控制函數(shù)時(shí),加載頁面和控制頁面要分開編寫,不然無法運(yùn)行runJavaScript函數(shù)
#加載坐標(biāo)軸
def load_echarts(self):
#清空布局,重新載入圖片
if self.verticalLayout_3.count() > 0:
self.verticalLayout_3.removeItem(self.verticalLayout_3.itemAt(0))
#echarts繪圖
self.weather = QWebEngineView()
self.weather.load(QUrl("file:///" + "weather.html")) # 注意格式,絕對(duì)路徑
self.verticalLayout_3.addWidget(self.weather)
self.statusBar().showMessage("圖像加載完畢")
def changeWeather(self):
self.label_19.setText("{}未來十五天氣溫走勢(shì)圖".format(self.ComboBox_2.currentText()))
if self.weather.page():
jscode = "weather({},{},{})".format(self.date_forecast,self.high,self.low)
self.weather.page().runJavaScript(jscode)
柚子快報(bào)邀請(qǐng)碼778899分享:pyqt與echart交互
參考閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。