pyQt中的evaluateJavaScript - 函数未被调用 [英] evaluateJavaScript in PyQt - function is not called

查看:483
本文介绍了pyQt中的evaluateJavaScript - 函数未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PyQt / Webview有问题。困扰我的代码应该呈现图表,但事实并非如此。我使用Highcharts框架并为通过Python呈现图表的javascript函数提供数据。我还检查webview是否准备好,因为这是我以前的错误。有问题的代码看起来像这样:

  command =('loadPinch('+ str(random.sample(range(10000 ),1000))+ 
','+ str(['2009','01','01'])+');')
self.webview.page()。mainFrame ).evaluateJavaScript(
var readyStateCheckInterval = setInterval(function(){
if(document.readyState ==='complete'){+
command +
clearInterval(readyStateCheckInterval);
}
},10);)

loadPinch是一个JavaScript函数,看起来与此类似(反过来看起来类似于一个Highcharts示例图):

 <$ c $ {
图表:{
zoomType:'x',
spacingRight:20
},
title:{text:'Twitter Trends'},
副标题: {
text:document.ontouchstart === undefined?
'点击并拖动绘图区域以放大':
'捏住图表以放大'
},
xAxis:{
type:'datetime ',
maxZoom:10 * 24 * 3600000,
title:{text:null}
},
yAxis:{
title:{text:'Occurence' }
},
tooltip:{shared:true},
legend:{enabled:false},
plotOptions:{
area:{
fillColor :{
linearGradient:{x1:0,y1:0,x2:0,y2:1},
stops:[
[0,Highcharts.getOptions()。colors [0] ],
[1,Highcharts.Color(Highcharts.getOptions()。colors [0])。setOpacity(0).get('rgba')]
]
},
lineWidth:1,
标记:{enabled:false},
shad ow:false,
states:{hover:{lineWidth:1}},
threshold:null
}
},
series:[{
类型:'area',
名称:'Twitter Trend',
pointInterval:24 * 3600 * 1000,
pointStart:Date.UTC(date [0],date [1],date [2]),
data:data
}]
});
};

我可能会告诉你,如果我打印命令,我将evaluateJavaScript交给控制台而不是执行并将其粘贴到开发者控制台,它会按预期执行并呈现图表,这导致我相信存在某种我无法追踪的竞争状态。



有人知道可能是什么问题吗?



问候,
卡森



注意:问题依然存在。

解决方案

我最终找到了我的修复程序;在JS中,我认为太多了。



QT中有一个信号在页面完成加载时发出,名为 loadFinished()。通过将此信号绑定到执行 evaluateJavascript(command)的调用的插槽,图表加载。看起来Qts webview在页面尚未完成加载时有问题来执行Javascript。



无论如何,这就是代码的外观(简化): ():
def __init __(self):
self.webview = QtWebkitWidgets.QWebView()
self._myBindingFunction()

def _myBindingFunction(self):
self.webview.loadFinished.connect(self._plot)

def _plot( self):
command =('loadPinch('+ str(random.sample(range(10000),1000))+
','+ str(['2009','01',' 01'])+');')
self.webview.page()。mainFrame()。evaluateJavaScript(command)

请注意这是如何简化JS代码的;我不必检查已加载的站点,因为在发出信号后,它必须准备就绪。



Pawel Fus 让我走上正轨。我已经放弃了。


I have a problem with PyQt/its Webview. The code that is bothering me should render a chart, but it doesn't. I am using the Highcharts framework and provide the data for the javascript function that renders the chart via Python. I also check whether the webview is ready because that was a previous error of mine. The code in question looks something like this:

command = ('loadPinch(' + str(random.sample(range(10000), 1000)) +
            ', ' + str(['2009', '01', '01']) + ');')
self.webview.page().mainFrame().evaluateJavaScript("""
        var readyStateCheckInterval = setInterval(function() {
            if(document.readyState === 'complete'){ """ +
                command + """
                clearInterval(readyStateCheckInterval);
            }
        }, 10);""")

loadPinch is a JavaScript function and looks similar to this(which in turn looks similar to a Highcharts example graph):

    function loadPinch(data, date){
    $('#container').highcharts({
        chart: {
            zoomType: 'x',
            spacingRight: 20
        },
        title: {text: 'Twitter Trends'},
        subtitle: {
            text: document.ontouchstart === undefined ?
                'Click and drag in the plot area to zoom in' :
                'Pinch the chart to zoom in'
        },
        xAxis: {
            type: 'datetime',
            maxZoom: 10 * 24 * 3600000,
            title: {text: null}
        },
        yAxis: {
            title: {text: 'Occurence'}
        },
        tooltip: {shared: true},
        legend: {enabled: false},
        plotOptions: {
            area: {
                fillColor: {
                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1},
                    stops: [
                        [0, Highcharts.getOptions().colors[0]],
                        [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                    ]
                },
                lineWidth: 1,
                marker: {enabled: false},
                shadow: false,
                states: {hover: {lineWidth: 1}},
                threshold: null
            }
        },
        series: [{
            type: 'area',
            name: 'Twitter Trend',
            pointInterval: 24 * 3600 * 1000,
            pointStart: Date.UTC(date[0], date[1], date[2]),
            data: data
        }]
    });
};

I might tell you that if I print the command that I am handing evaluateJavaScript to the console instead of executing it and pasting it over to the developer console, it gets executed as expected and renders the chart which leads me to believe that there is some sort of race condition I can't track down.

Does someone have an idea of what could be the problem?

Regards, Carson

NOTE: The problem is still relevant.

解决方案

I eventually found my fix; i thought too much in JS terms.

There is a signal in QT that is emitted when the page is finished loading, called loadFinished(). By binding this signal to a slot of mine which executed the call to evaluateJavascript(command), the chart loaded. It seems like Qts webview has problems to execute Javascript when the page hasn't finished loading yet.

Anyway, this is how the code looks like(simplified):

class something():
    def __init__(self):
        self.webview = QtWebkitWidgets.QWebView()
        self._myBindingFunction()

    def _myBindingFunction(self):
        self.webview.loadFinished.connect(self._plot)

    def _plot(self):
        command = ('loadPinch(' + str(random.sample(range(10000), 1000)) +
                   ', ' + str(['2009', '01', '01']) + ');')
        self.webview.page().mainFrame().evaluateJavaScript(command)

Note how that simplifies the JS code; i do not have to check for the site having loaded, because after the signal is emitted, it has to be ready.

Big shoutout to Pawel Fus though for leading me onto the right track. I was already giving it up.

这篇关于pyQt中的evaluateJavaScript - 函数未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆