[英]sudo apt install python3-pyqt5 and other apt install methods
here is the error.这是错误。
QWebView is not defined
I am following along from a PyQt4 tutorial at https://wiki.python.org/moin/PyQt/Embedding%20Widgets%20in%20Web%20Pages .我正在关注https://wiki.python.org/moin/PyQt/Embedding%20Widgets%20in%20Web%20Pages 上的 PyQt4 教程。
I understand defining but what exactly should I define here and where should I define it?我理解定义,但我应该在这里定义什么以及我应该在哪里定义它?
Where would I install QWebView?我将在哪里安装 QWebView?
I know the link provided is a PyQt4 example and I am using PyQt5.我知道提供的链接是 PyQt4 示例,我使用的是 PyQt5。 I know there have been changes but I have not found exactly what the answer could be now.我知道已经发生了变化,但我现在还没有找到确切的答案。
Here is some code:这是一些代码:
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_())
To use QtWebkit you have to install the package python3-pyqt5.qtwebkit
.要使用 QtWebkit,您必须安装软件包python3-pyqt5.qtwebkit
。 On the other hand, the main change from Qt4 to Qt5 was the reorganization of the modules, and that happened with QtWebKit that was divided into QtWebKit and QtWebkitWidgets, so QWebView belongs to the last sub-module:另一方面,从 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_())
Note: Since Qt 5.6 QtWebkit is no longer officially maintained (there is a fork maintained by the community) as it was replaced by QtWebEngine so to use QtWebkit you must use the official repositories of each distribution or compile it manually.注意:由于 Qt 5.6 QtWebkit 不再官方维护(社区维护了一个分支),因为它被 QtWebEngine 取代,所以要使用 QtWebkit,您必须使用每个发行版的官方存储库或手动编译它。 The functionality of the QWebPluginFactory cannot be implemented by QtWebEngine since the rendering is not made by Qt but by chromium. QWebPluginFactory 的功能不能由 QtWebEngine 实现,因为渲染不是由 Qt 制作的,而是由铬制作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.