python调用html内的js方法

这方面资料不多,不懂html,不懂js,略懂python的我,稍微看了点html和js,好几天的摸索,终于测试成功了。
PYQT+HTML利用PYQT的webview调用JS内方法
1.python调用js需要使用webView.page().mainFrame().evaluateJavaScript()这个方法
2.这个方法需要制作信号和槽才能触发。

功能:python调用html内的js定位函数。实现在webview内显示定位
python调用html内的js方法_第1张图片

测试代码mainUI.py

# -*- coding: utf-8 -*-
# 作者:神秘藏宝室
from Ui_mainUI import Ui_Dialog

#添加
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class MyBrowser(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
        htmlfile = "gpsPoint.html"
        url = QUrl(htmlfile)
        self.webView.load(url)
        self.webView.loadFinished.connect(self._plot)

        print u"html载入"
        # self.createConnection()

    # def createConnection(self):
    #     # self.connect(self.pushButtonGo,SIGNAL("clicked"),self.on_pushButtonGo_clicked)
    #     self.pushButtonGo.clicked.connect(self.on_pushButtonGo_clicked)


    @pyqtSlot(result="QString")
    def _plot(self):
        self.webView.page().mainFrame().evaluateJavaScript('theNewLocation(112.424483,34.640631);')



    @pyqtSignature("")
    def on_lineEditAddress_returnPressed(self):
        """
        Slot documentation goes here.
        """
        self.search()

    
    @pyqtSignature("")
    def on_pushButtonGo_clicked(self):
        """
        Slot documentation goes here.
        """
        self.search()


    def search(self):
        address = str(self.lineEditAddress.text())
        if address:
            if address.find('://') == -1:
                address = 'http://' + address
                self.lineEditAddress.setText(address)
            url = QUrl(address)
            self.webView.load(url)



if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    mb = MyBrowser()
    mb.show()
    sys.exit(app.exec_())

gpsPoint.html文件


        
        
            
            
            
            
            经纬度定位
        
        
            

转载于:https://www.cnblogs.com/Mysterious/p/10105923.html

你可能感兴趣的:(python调用html内的js方法)