How to put result of JavaScript function into python variable. PyQt

前端 未结 1 586
慢半拍i
慢半拍i 2021-02-09 19:24

I want to make a function in PyQt evaluateJavaScript() (or may be similar one) and than display a result of evaluated function. Real function will be much bigger, and it might

相关标签:
1条回答
  • 2021-02-09 19:33

    In this example first I create a myWindow javascript object by passing self to the main frame, then call evaluateJavaScript when loadFinished:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    from PyQt4 import QtCore, QtGui, QtWebKit  
    
    getJsValue = """ 
    w = document.getElementsByTagName('p')[0];
    myWindow.showMessage(w.innerHTML);
    """  
    
    class myWindow(QtWebKit.QWebView):  
        def __init__(self, parent=None):
            super(myWindow, self).__init__(parent)
    
            self.page().mainFrame().addToJavaScriptWindowObject("myWindow", self)
    
            self.loadFinished.connect(self.on_loadFinished)
    
            self.load(QtCore.QUrl('http://jquery.com'))
    
        @QtCore.pyqtSlot(str)  
        def showMessage(self, message):
            print "Message from website:", message
    
        @QtCore.pyqtSlot()
        def on_loadFinished(self):
            self.page().mainFrame().evaluateJavaScript(getJsValue) 
    
    if __name__ == "__main__":
        import sys
    
        app = QtGui.QApplication(sys.argv)
        app.setApplicationName('myWindow')
    
        main = myWindow()
        main.show()
    
        sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题