【问题标题】:How do I load/display an html file into my QTextBrowser widget?如何将 html 文件加载/显示到我的 QTextBrowser 小部件中?
【发布时间】:2019-10-29 11:21:42
【问题描述】:

我正在自学如何使用 PyQt5 在 python 中编写 UI。我想做的一件事是获取与我的应用程序保存在同一文件夹中的 html 文档并显示其内容。看起来 QTextBrowser 是加载/显示 html 文档的正确小部件,但我无法弄清楚要使用什么命令以及如何使用它。如果这是一个愚蠢的问题,我很抱歉,但我对 Python 和 UI 编码还是新手,所以我无法理解文档以及我做错了什么。

QTextBrowser 的文档中提到了加载文档的方法的 QUrl、setSource 和 source。我已经尝试将我的 html 文档的名称放入其中,但它们都不起作用。 userSet 是根据用户输入确定的数据集,用户输入和数据生成工作正常,因为我能够使用 QTableView 小部件显示数据表。

import sys
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QTableWidget, QTableWidgetItem

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.submitButton.clicked.connect(self.handleInput)
        self.htmlView = QtWidgets.QTextBrowser(self)

    def handleInput(self):
        #Display Hexbin
        p = figure(plot_width = 300, plot_height = 300)
        p.hexbin(userSet.day, userSet.score, size = 1)
        html = output_file("userHexbin.html")
        save(p)
        self.oLayout.addWidget(self.htmlView)
        self.htmlView.source("userHexbin.html")

我希望应用程序显示我保存在 userHexbin.html 的 hexbin 图,但我收到以下错误 - TypeError: source(self): too many arguments。我不知道我应该把我的文档名称放在哪里。

编辑:

from bokeh.plotting import figure, output_file, show, save
from bokeh.resources import CDN
import pandas as pd
import sys
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from PyQt5.QtWidgets import *
app = QApplication([])
button = QPushButton('Submit')
def on_button_clicked():
    p = figure(plot_width = 300, plot_height = 300)
    data = {'Day':[0, 1, 2, 3, 0, 1], 'Num':[0, 0, 1, 1, 2, 3]}
    df = pd.DataFrame(data)
    p.hexbin(df.Day, df.Num, size = .5) 
    html = output_file("test.html")
    save(p)
    output = QTextBrowser()
    output.setSource(QtCore.QUrl.fromLocalFile("test.html"))

button.clicked.connect(on_button_clicked)
button.show()
app.exec_()

【问题讨论】:

    标签: python pyqt pyqt5 bokeh qtextbrowser


    【解决方案1】:

    Qt 有命名其方法的约定:

    • 吸气剂:property()
    • 二传手:setProperty()

    在您的情况下,source() 是您不想要的吸气剂,您必须使用 setSource()。

    另一方面,setSource() 需要 QUrl,因此您必须使用 QUrl.fromLocalFile(...) 从路径转换。

    self.htmlView.setSource(QtCore.QUrl.fromLocalFile("userHexbin.html"))
    

    QTextBrowser 不支持 javascript,所以它不是正确的小部件来显示它,在这种情况下我建议使用 QWebEngineView (安装它使用pip install PyQtWebEngine),也不需要创建文件,你可以加载直接:

    import pandas as pd
    from bokeh import plotting, embed, resources
    from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
    
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(Widget, self).__init__(parent)
    
            button = QtWidgets.QPushButton("Submit")
            self.m_output = QtWebEngineWidgets.QWebEngineView()
    
            button.clicked.connect(self.on_button_clicked)
    
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(button)
            lay.addWidget(self.m_output)
            self.resize(640, 480)
    
        @QtCore.pyqtSlot()
        def on_button_clicked(self):
            p = plotting.figure(plot_width=300, plot_height=300)
            data = {"Day": [0, 1, 2, 3, 0, 1], "Num": [0, 0, 1, 1, 2, 3]}
            df = pd.DataFrame(data)
            p.hexbin(df.Day, df.Num, size=0.5)
            html = embed.file_html(p, resources.CDN, "my plot")
            self.m_output.setHtml(html)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
    
        w = Widget()
        w.show()
    
        sys.exit(app.exec_())
    

    【讨论】:

    • 哦,这更有意义!进行该更改阻止了错误的发生,但情节仍未显示。该文件的情节保存得很好。我错过了显示命令还是 QTextBrowser 与散景图不兼容?
    • @wooloop 我无法回答任何问题,因为您的代码信息很少,如果您需要帮助,您必须提供minimal reproducible example
    • 我用一个通用示例编辑了原始帖子,该示例具有保存工作散景图但不显示它的相同问题。这行得通吗?
    • 我能够使用给定的命令进行安装,甚至仔细检查了 QtWebEngineWidgets 是否在我的 PyQt5 文件夹中,但是导入会导致以下错误 - ImportError: cannot import name 'QtWebEngineWidgets' from 'PyQt5'。重新安装 PyQt 和相关软件包并不能修复此错误(如果重要的话,我使用的是 Windows 10 笔记本电脑)。你知道是否还有其他 Widget 可以工作吗?
    • 以管理员身份执行python -m pip install pyqt5 PyQtWebEngine,您的PC 是32 位还是64 位?你在使用任何 IDE 吗?如果是这样,您确定您的 IDE 使用的是安装了 pyqtwebengine 的 python 吗?
    猜你喜欢
    • 2020-11-19
    • 1970-01-01
    • 2023-02-04
    • 2019-01-01
    • 2013-01-25
    • 1970-01-01
    • 2016-04-23
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多