PyQt5最全66 Web之装载本地Web页面
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
import sys
import os
class WebEngineView(QMainWindow):
"""
装载本地Web界面
load fromLocalFile
"""
def __init__(self):
super(WebEngineView, self).__init__()
self.setWindowTitle('装载本地Web界面')
self.setGeometry(5, 30, 1355, 730)
# 获得当前目录
url = os.getcwd() + '/test.html'
print(url)
self.browser = QWebEngineView()
self.browser.load(QUrl.fromLocalFile(url))
self.setCentralWidget(self.browser)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = WebEngineView()
print(main.__doc__)
main.show()
sys.exit(app.exec_())
下面是test.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试界面</title>
</head>
<body>
<h1>Hello PyQt5</h1>
<h2>Hello PyQt5</h2>
<h3>Hello PyQt5</h3>
<h4>Hello PyQt5</h4>
</body>
</html>