如何将 html 文件加载/显示到我的 QTextBrowser 小部件中?

问题描述 投票:0回答:2

我正在自学如何使用 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): 参数太多。我不知道我还应该把我的文档名称放在哪里。

编辑:

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
2个回答
3
投票

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_())


0
投票
output = QTextBrowser()
output.setHtml("test.html")
© www.soinside.com 2019 - 2024. All rights reserved.