这篇文章次要介绍了 PyQt5 的 QWebEngineView 应用示例,帮忙大家更好的学习和应用 python,感兴趣的敌人能够理解下
一.反对视频播放
self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True) #反对视频播放
self.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True) #反对视频播放
二.反对页面敞开申请
self.page().windowCloseRequested.connect(self.on_windowCloseRequested) #页面敞开申请
三.反对页面下载申请
self.page().profile().downloadRequested.connect(self.on_downloadRequested) #页面下载申请
【如下代码,齐全复制,间接运行,即可应用】
import sys
import os
import datetime
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView,QWebEngineSettings
# 调试窗口配置
# 如果不想本人创立调试窗口,能够应用 Chrome 连贯这个地址进行调试
DEBUG_PORT = '5588'
DEBUG_URL = 'http://127.0.0.1:%s' % DEBUG_PORT
os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = DEBUG_PORT
################################################
####### 创立主窗口
################################################
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('My Browser')
#self.showMaximized()
self.setWindowFlags(Qt.FramelessWindowHint)
##### 创立 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 必须要有,是将主窗口作为参数,传给浏览器
self.webview.load(QUrl("https://www.baidu.com"))
self.create_tab(self.webview)
#网页调试窗口
self.inspector = QWebEngineView()
self.inspector.setWindowTitle('Web Inspector')
self.inspector.load(QUrl(DEBUG_URL))
self.webview.loadFinished.connect(self.handleHtmlLoaded)
# 加载实现后显示调试网页
def handleHtmlLoaded(self, ok):
if ok:
self.webview.page().setDevToolsPage(self.inspector.page())
self.inspector.show()
#创立 tab
def create_tab(self,webview):
self.tab = QWidget()
self.tabWidget.addTab(self.tab, "新标签页")
self.tabWidget.setCurrentWidget(self.tab)
#####
self.Layout = QHBoxLayout(self.tab)
self.Layout.setContentsMargins(0, 0, 0, 0)
self.Layout.addWidget(webview)
#敞开 tab
def close_Tab(self,index):
if self.tabWidget.count()>1:
self.tabWidget.removeTab(index)
else:
self.close() # 当只有 1 个 tab 时,敞开主窗口
################################################
####### 创立浏览器
################################################
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) #页面敞开申请
self.page().profile().downloadRequested.connect(self.on_downloadRequested) #页面下载申请
# 反对页面敞开申请
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)
# 下载完结触发函数
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())