首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sudo apt安装python3 3-pyqt5 5和其他apt安装方法

sudo apt安装python3 3-pyqt5 5和其他apt安装方法
EN

Stack Overflow用户
提问于 2020-10-04 21:50:59
回答 1查看 389关注 0票数 1

这是错误。

代码语言:javascript
复制
QWebView is not defined

我是从PyQt4的https://wiki.python.org/moin/PyQt/Embedding%20Widgets%20in%20Web%20Pages教程中学习的。

我理解定义,但是我到底应该在这里定义什么,应该在哪里定义呢?

我将在哪里安装QWebView?

我知道提供的链接是一个PyQt4示例,我正在使用PyQt5。我知道已经发生了变化,但我还没有找到确切的答案。

以下是一些代码:

代码语言:javascript
复制
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_())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-04 22:24:55

要使用QtWebkit,您必须安装包python3-pyqt5.qtwebkit。另一方面,从Qt4到Qt5的主要变化是模块的重组,而QtWebKit被划分为QtWebKit和QtWebkitWidgets,因此QWebView属于最后一个子模块:

代码语言:javascript
复制
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生成的,而是由铬完成的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64200245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档