PyQt5控件QWebEngineView(WebView)
PyQt5控件QWebEngineView(WebView)
下载依赖
PyQt5
、PyQtWebEngine
pip install --index-url=https://mirrors.aliyun.com/pypi/simple/ PyQt5
pip install --index-url=https://mirrors.aliyun.com/pypi/simple/ PyQtWebEngine
加载外部网页
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('加载外部网页')
self.setGeometry(5, 30, 1355, 730)
self.browser = QWebEngineView()
# 加载外部的web界面
self.browser.load(QUrl('https://blog.csdn.net/qq_59636442?type=blog'))
self.setCentralWidget(self.browser)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
app.exit(app.exec_())
加载HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('加载本地网页的例子')
self.setGeometry(5,30,1355,730) # 设置窗口的位置和大小(x, y, width, height)
self.browser=QWebEngineView()
# 加载本地的HTML界面
url=r'file:///./template/test.html'
self.browser.load(QUrl(url))
self.setCentralWidget(self.browser)
if __name__ == '__main__':
app=QApplication(sys.argv) # 创建一个QApplication对象,sys.argv参数确保了命令行参数能够传递给应用程序
win=MainWindow()
win.show()
app.exit(app.exec_()) # 进入应用程序的主事件循环,直到应用程序退出。app.exec_()是一个阻塞调用,直到退出事件循环
print("程序退出")