QWebEngineView常用方法
方法 | 描述 |
---|---|
load(QUrl url) | 加载指定的URL并显示 |
setHtml(QString &html) | 将网页视图的内容设置为指定的HTML内容 |
核心代码:
- view = QWebEngineView()
- view.load(QUrl(‘http://www.xxx.com’))
- view.show()
加载并显示外部的Web页面
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle('打开外部网页 Demo')
self.setGeometry(5, 30, 1024, 730)
self.browser = QWebEngineView()
self.browser.load(QUrl('https://www.cnblogs.com/wangshuo1'))
self.setCentralWidget(self.browser)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()
加载并显示本地的Web页面
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(QMainWindow, self).__init__()
self.setWindowTitle("加载并显示本地页面Demo")
self.setGeometry(5, 30, 555, 330)
self.browser = QWebEngineView()
# 加载本地页面
url = r'D:/project/python/pyqt5/web/index.html'
self.browser.load(QUrl(url))
self.setCentralWidget(self.browser)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试页面</title>
</head>
<body>
<h1>Hello PyQt5</h1>
<h1>Java</h1>
<h1>Android</h1>
<h1>Linux</h1>
</body>
</html>
文章来源:https://www.toymoban.com/news/detail-418412.html
加载并显示嵌入的HTML代码
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(QMainWindow, self).__init__()
self.setWindowTitle("加载并显示本地页面")
self.setGeometry(5, 30, 1355, 730)
self.browser = QWebEngineView()
self.browser.setHtml('''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试页面</title>
</head>
<body>
<h1>Hello PyQt5</h1>
<h1>Java</h1>
<h1>Android</h1>
<h1>Linux</h1>
<h1>C/C++</h1>
</body>
</html>
''')
self.setCentralWidget(self.browser)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
文章来源地址https://www.toymoban.com/news/detail-418412.html
到了这里,关于PyQt5 QWebEngineView网页交互的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!