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

时间:2019-06-14 20:17:46

标签: python pyqt pyqt5 bokeh qtextbrowser

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

QTextBrowser的文档提到了QUrl,setSource和用于加载文档的方法的源。我尝试将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上的六边形图,但出现以下错误-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_()

1 个答案:

答案 0 :(得分:2)

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

enter image description here

相关问题