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

德功能

这是错误。

QWebView is not defined

我正在通过https://wiki.python.org/moin/PyQt/Embedding%20Widgets%20in%20Web%20Pages的PyQt4教程进行学习

我了解定义,但是我应该在这里定义什么,在哪里定义?

我将在哪里安装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_())
永乐

要使用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_())

注意:由于Qt 5.6已不再正式维护QtWebkit(社区维护了一个fork),因为它已被QtWebEngine取代,因此要使用QtWebkit,必须使用每个发行版的官方存储库或手动进行编译。QtWebEngine不能实现QWebPluginFactory的功能,因为渲染不是由Qt而是由铬进行的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

sudo apt-get install 无法安装 plank 和其他应用程序

创建简单的命令来安装sudo apt?

sudo apt 无法安装软件包

错误“ sudo apt-get -f安装”

`pip install pyqt5` 和 `sudo apt-get install python3-pyqt5` 之间有什么区别?[这里pyQt5本身更像是一个占位符而不是任何东西]

无法使用 sudo apt-get install 安装或找到 python-pip 和 python-requests

当我们执行 sudo apt-get upgrade python3 时会发生什么

sudo apt-get -f安装后出现错误

为什么目录权限阻止使用文件的“ sudo apt安装”?

无法安装(sudo apt --fix-broken install)

Django 既通过 python3 安装,又通过 apt-get 安装。我可以卸载apt-get吗?

sudo apt-get <下载/安装> python-opencv获取与预期不同的版本

sudo pip安装Django

Sudo安装问题

apt-get -f install建议删除python2.7并安装python3

通过apt-get`sudo apt-get install openblas-dev`安装openblas

sudo composer安装与composer安装

无法使用 sudo apt 更新

Sudo apt-get 更新

从下载的 .deb 文件安装和从命令“sudo apt-get install”安装有什么区别?

dpkg,sudo,tty和apt损坏

何时运行“sudo apt update”,何时运行“sudo apt update && sudo apt upgrade”?

无法在Gitlab CI中使用pip3安装python3 PyQt5模块

sudo pip安装错误

安装没有sudo的jdk?

无需sudo进行安装*之后*

如何使用apt-get安装其他Python版本?

sudo apt-get install和sudo apt-f install返回错误

Ubuntu sudo apt-get upgrade和sudo apt-get install之间的区别?