PyQt5的QWebEngineView使用示例
一.支持視頻播放
關(guān)鍵代碼
self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True) #支持視頻播放
二.支持頁面關(guān)閉請求
關(guān)鍵代碼
self.page().windowCloseRequested.connect(self.on_windowCloseRequested) #頁面關(guān)閉請求
三.支持頁面下載請求
關(guān)鍵代碼
self.page().profile().downloadRequested.connect(self.on_downloadRequested) #頁面下載請求
完整源碼
【如下代碼,完全復(fù)制,直接運(yùn)行,即可使用】
import sys import os import datetime from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtWebEngineWidgets import QWebEngineView,QWebEngineSettings # 調(diào)試窗口配置 # 如果不想自己創(chuàng)建調(diào)試窗口,可以使用Chrome連接這個地址進(jìn)行調(diào)試 DEBUG_PORT = '5588' DEBUG_URL = 'http://127.0.0.1:%s' % DEBUG_PORT os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = DEBUG_PORT ################################################ #######創(chuàng)建主窗口 ################################################ class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('My Browser') #self.showMaximized() self.setWindowFlags(Qt.FramelessWindowHint) #####創(chuàng)建tabwidget self.tabWidget = QTabWidget() self.tabWidget.setTabShape(QTabWidget.Triangular) self.tabWidget.setDocumentMode(True) self.tabWidget.setMovable(True) self.tabWidget.setTabsClosable(True) self.tabWidget.tabCloseRequested.connect(self.close_Tab) self.setCentralWidget(self.tabWidget) ####第一個tab self.webview = WebEngineView(self) #self必須要有,是將主窗口作為參數(shù),傳給瀏覽器 self.webview.load(QUrl("https://www.baidu.com")) self.create_tab(self.webview) #網(wǎng)頁調(diào)試窗口 self.inspector = QWebEngineView() self.inspector.setWindowTitle('Web Inspector') self.inspector.load(QUrl(DEBUG_URL)) self.webview.loadFinished.connect(self.handleHtmlLoaded) # 加載完成后顯示調(diào)試網(wǎng)頁 def handleHtmlLoaded(self, ok): if ok: self.webview.page().setDevToolsPage(self.inspector.page()) self.inspector.show() #創(chuàng)建tab def create_tab(self,webview): self.tab = QWidget() self.tabWidget.addTab(self.tab, "新標(biāo)簽頁") self.tabWidget.setCurrentWidget(self.tab) ##### self.Layout = QHBoxLayout(self.tab) self.Layout.setContentsMargins(0, 0, 0, 0) self.Layout.addWidget(webview) #關(guān)閉tab def close_Tab(self,index): if self.tabWidget.count()>1: self.tabWidget.removeTab(index) else: self.close() # 當(dāng)只有1個tab時,關(guān)閉主窗口 ################################################ #######創(chuàng)建瀏覽器 ################################################ class WebEngineView(QWebEngineView): def __init__(self,mainwindow,parent=None): super(WebEngineView, self).__init__(parent) self.mainwindow = mainwindow ############## self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True) #支持視頻播放 self.page().windowCloseRequested.connect(self.on_windowCloseRequested) #頁面關(guān)閉請求 self.page().profile().downloadRequested.connect(self.on_downloadRequested) #頁面下載請求 # 支持頁面關(guān)閉請求 def on_windowCloseRequested(self): the_index = self.mainwindow.tabWidget.currentIndex() self.mainwindow.tabWidget.removeTab(the_index) # 支持頁面下載按鈕 def on_downloadRequested(self,downloadItem): if downloadItem.isFinished()==False and downloadItem.state()==0: ###生成文件存儲地址 the_filename = downloadItem.url().fileName() if len(the_filename) == 0 or "." not in the_filename: cur_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S') the_filename = "下載文件" + cur_time + ".xls" the_sourceFile = os.path.join(os.getcwd(), the_filename) ###下載文件 # downloadItem.setSavePageFormat(QWebEngineDownloadItem.CompleteHtmlSaveFormat) downloadItem.setPath(the_sourceFile) downloadItem.accept() downloadItem.finished.connect(self.on_downloadfinished) # 下載結(jié)束觸發(fā)函數(shù) def on_downloadfinished(self): js_string = ''' alert("下載成功,請到軟件同目錄下,查找下載文件!"); ''' self.page().runJavaScript(js_string) # 重寫createwindow() def createWindow(self, QWebEnginePage_WebWindowType): new_webview = WebEngineView(self.mainwindow) self.mainwindow.create_tab(new_webview) return new_webview ################################################ #######程序入門 ################################################ if __name__ == "__main__": app = QApplication(sys.argv) the_mainwindow = MainWindow() the_mainwindow.show() sys.exit(app.exec())
以上就是PyQt5的QWebEngineView使用示例的詳細(xì)內(nèi)容,更多關(guān)于PyQt5的QWebEngineView的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python利用matplotlib.pyplot.boxplot()繪制箱型圖實(shí)例代碼
相信大家應(yīng)該都知道Python繪制箱線圖主要用matplotlib庫里pyplot模塊里的boxplot()函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python利用matplotlib.pyplot.boxplot()繪制箱型圖的相關(guān)資料,需要的朋友可以參考下2022-08-08利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的操作實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-08-08python基于event實(shí)現(xiàn)線程間通信控制
這篇文章主要介紹了python基于event實(shí)現(xiàn)線程間通信控制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01Python 數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09