1

PyQt5 を使用して Python で UI をコーディングする方法を独学しています。私がやりたいことの 1 つは、アプリと同じフォルダーに保存されている 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_()
4

1 に答える 1