这是错误。
QWebView is not defined我是从PyQt4的https://wiki.python.org/moin/PyQt/Embedding%20Widgets%20in%20Web%20Pages教程中学习的。
我理解定义,但是我到底应该在这里定义什么,应该在哪里定义呢?
我将在哪里安装QWebView?
我知道提供的链接是一个PyQt4示例,我正在使用PyQt5。我知道已经发生了变化,但我还没有找到确切的答案。
以下是一些代码:
import sys
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import *
from PyQt5.QtWebKit import *
from PyQt5.QtWidgets import *
html = \
"""<html>
<head>
<title>Python Web Plugin Test</title>
</head>
<body>
<h1>Python Web Plugin Test</h1>
<object type="x-pyqt/widget" width="200" height="200"></object>
<p>This is a Web plugin written in Python.</p>
</body>
</html>
"""
class WebWidget(QWidget):
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setBrush(Qt.white)
painter.setPen(Qt.black)
painter.drawRect(self.rect().adjusted(0, 0, -1, -1))
painter.setBrush(Qt.red)
painter.setPen(Qt.NoPen)
painter.drawRect(self.width()/4, self.height()/4,
self.width()/2, self.height()/2)
painter.end()
def sizeHint(self):
return QSize(100, 100)
class WebPluginFactory(QWebPluginFactory):
def __init__(self, parent = None):
QWebPluginFactory.__init__(self, parent)
def create(self, mimeType, url, names, values):
if mimeType == "x-pyqt/widget":
return WebWidget()
def plugins(self):
plugin = QWebPluginFactory.Plugin()
plugin.name = "PyQt Widget"
plugin.description = "An example Web plugin written with PyQt."
mimeType = QWebPluginFactory.MimeType()
mimeType.name = "x-pyqt/widget"
mimeType.description = "PyQt widget"
mimeType.fileExtensions = []
plugin.mimeTypes = [mimeType]
print("plugins")
return [plugin]
if __name__ == "__main__":
app = QApplication(sys.argv)
QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
view = QWebView()
factory = WebPluginFactory()
view.page().setPluginFactory(factory)
view.setHtml(html)
view.show()
sys.exit(app.exec_())发布于 2020-10-04 22:24:55
要使用QtWebkit,您必须安装包python3-pyqt5.qtwebkit。另一方面,从Qt4到Qt5的主要变化是模块的重组,而QtWebKit被划分为QtWebKit和QtWebkitWidgets,因此QWebView属于最后一个子模块:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWebKit import QWebPluginFactory, QWebSettings
from PyQt5.QtWebKitWidgets import QWebView
html = """<html>
<head>
<title>Python Web Plugin Test</title>
</head>
<body>
<h1>Python Web Plugin Test</h1>
<object type="x-pyqt/widget" width="200" height="200"></object>
<p>This is a Web plugin written in Python.</p>
</body>
</html>
"""
class WebWidget(QWidget):
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setBrush(Qt.white)
painter.setPen(Qt.black)
painter.drawRect(self.rect().adjusted(0, 0, -1, -1))
painter.setBrush(Qt.red)
painter.setPen(Qt.NoPen)
painter.drawRect(
self.width() / 4, self.height() / 4, self.width() / 2, self.height() / 2
)
painter.end()
def sizeHint(self):
return QSize(100, 100)
class WebPluginFactory(QWebPluginFactory):
def __init__(self, parent=None):
QWebPluginFactory.__init__(self, parent)
def create(self, mimeType, url, names, values):
if mimeType == "x-pyqt/widget":
return WebWidget()
def plugins(self):
plugin = QWebPluginFactory.Plugin()
plugin.name = "PyQt Widget"
plugin.description = "An example Web plugin written with PyQt."
mimeType = QWebPluginFactory.MimeType()
mimeType.name = "x-pyqt/widget"
mimeType.description = "PyQt widget"
mimeType.fileExtensions = []
plugin.mimeTypes = [mimeType]
print("plugins")
return [plugin]
if __name__ == "__main__":
app = QApplication(sys.argv)
QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
view = QWebView()
factory = WebPluginFactory()
view.page().setPluginFactory(factory)
view.setHtml(html)
view.show()
sys.exit(app.exec_())注释:,因为QT5.6QtWebkit不再被正式维护(社区维护了一个分支),因为它被QtWebEngine所取代,所以要使用QtWebkit,您必须使用每个发行版的官方存储库,或者手动编译它。QWebPluginFactory的功能不能由QtWebEngine实现,因为呈现不是由Qt生成的,而是由铬完成的。
https://stackoverflow.com/questions/64200245
复制相似问题