PyQt5的QWebEngineView使用示例
一.支持視頻播放
關(guān)鍵代碼
self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True) #支持視頻播放
二.支持頁(yè)面關(guān)閉請(qǐng)求
關(guān)鍵代碼
self.page().windowCloseRequested.connect(self.on_windowCloseRequested) #頁(yè)面關(guān)閉請(qǐng)求
三.支持頁(yè)面下載請(qǐng)求
關(guān)鍵代碼
self.page().profile().downloadRequested.connect(self.on_downloadRequested) #頁(yè)面下載請(qǐng)求
完整源碼
【如下代碼,完全復(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連接這個(gè)地址進(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)
####第一個(gè)tab
self.webview = WebEngineView(self) #self必須要有,是將主窗口作為參數(shù),傳給瀏覽器
self.webview.load(QUrl("https://www.baidu.com"))
self.create_tab(self.webview)
#網(wǎng)頁(yè)調(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)頁(yè)
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)簽頁(yè)")
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個(gè)tab時(shí),關(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) #頁(yè)面關(guān)閉請(qǐng)求
self.page().profile().downloadRequested.connect(self.on_downloadRequested) #頁(yè)面下載請(qǐng)求
# 支持頁(yè)面關(guān)閉請(qǐng)求
def on_windowCloseRequested(self):
the_index = self.mainwindow.tabWidget.currentIndex()
self.mainwindow.tabWidget.removeTab(the_index)
# 支持頁(yè)面下載按鈕
def on_downloadRequested(self,downloadItem):
if downloadItem.isFinished()==False and downloadItem.state()==0:
###生成文件存儲(chǔ)地址
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("下載成功,請(qǐng)到軟件同目錄下,查找下載文件!");
'''
self.page().runJavaScript(js_string)
# 重寫(xiě)createwindow()
def createWindow(self, QWebEnginePage_WebWindowType):
new_webview = WebEngineView(self.mainwindow)
self.mainwindow.create_tab(new_webview)
return new_webview
################################################
#######程序入門(mén)
################################################
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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5中QWebEngineView內(nèi)嵌網(wǎng)頁(yè)與Python的數(shù)據(jù)交互傳參詳細(xì)方法實(shí)例
- python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5瀏覽器控件QWebEngineView詳細(xì)使用方法
- pyqt5的QWebEngineView 使用模板的方法
- PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例
- 如何讓PyQt5中QWebEngineView與JavaScript交互
相關(guān)文章
Python利用matplotlib.pyplot.boxplot()繪制箱型圖實(shí)例代碼
相信大家應(yīng)該都知道Python繪制箱線(xiàn)圖主要用matplotlib庫(kù)里pyplot模塊里的boxplot()函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python利用matplotlib.pyplot.boxplot()繪制箱型圖的相關(guān)資料,需要的朋友可以參考下2022-08-08
利用Python批量導(dǎo)出mysql數(shù)據(jù)庫(kù)表結(jié)構(gòu)的操作實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Python批量導(dǎo)出mysql數(shù)據(jù)庫(kù)表結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-08-08
python基于event實(shí)現(xiàn)線(xiàn)程間通信控制
Python 數(shù)據(jù)結(jié)構(gòu)之樹(shù)的概念詳解
Python SQLAlchemy入門(mén)教程(基本用法)

