QWebEngineView常用方法
方法 | 描述 |
---|---|
load(QUrl url) | 加載指定的URL并顯示 |
setHtml(QString &html) | 將網(wǎng)頁視圖的內(nèi)容設(shè)置為指定的HTML內(nèi)容 |
核心代碼:
- 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('打開外部網(wǎng)頁 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>
文章來源:http://www.zghlxwxcb.cn/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_())
文章來源地址http://www.zghlxwxcb.cn/news/detail-418412.html
到了這里,關(guān)于PyQt5 QWebEngineView網(wǎng)頁交互的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!