使用PyQt4显示HTML页面时出错 - QWebView

时间:2016-06-30 12:32:52

标签: python html pyqt4 bokeh qwebview

根据以下SO文章:How to display html using QWebView. Python?

我修改了安德烈的以下代码:

import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class Browser(QWebView):

    def __init__(self):
        QWebView.__init__(self)
        self.loadFinished.connect(self._result_available)

    def _result_available(self, ok):
        frame = self.page().mainFrame()
        # print(unicode(frame.toHtml()).encode('utf-8'))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = Browser()
    view.load(QUrl('http://www.google.com'))
    view.show()
    app.exec_()

然而,由于一些奇怪的原因,我仍然无法在我的GUI窗口中查看Google。我得到下面的屏幕(尽管等了5分钟并且有完整的互联网连接)

See screenshot here

更重要的是,我正在尝试查看使用Bokeh生成的离线HTML文件。

from bokeh.plotting import figure, output_file, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# add a line renderer with legend and line thickness
p.line(x, y, legend="Temp.", line_width=2)

# show the results
show(p)

使用离线html文件正确替换GUI脚本:

htmlPath = QUrl('line_example.html')
view.load(htmlPath)

我仍然看不到我生成的HTML文件 - 显示与以前相同的空白窗口。

我也尝试过如下定义htmlPath,但他们仍然无法工作:

htmlPath = QUrl("file:///C:/Users/giranm/PycharmProjects/Dashboard%20Testing/lineGraph.html")

htmlPath = QUrl.fromLocalFile(QFileInfo("lineGraph.html").absoluteFilePath())

非常感谢任何获得上述工作的帮助。

1 个答案:

答案 0 :(得分:0)

请参阅此答案:Bokeh plots do not display in QWebView

TLDR:显然QWebView将无法运行加载BokehJS所需的<script>个标签(这是实际完成所有工作以呈现内容的JavaScript库)内联资源可能是一个选项,但请注意,Bokeh不会以任何有意义的方式“支持”QwebView