python pyqt5 QtWebEngineWidgets 调用JS

2019-07-22 13:41:37 浏览数 (1)

-- coding: utf-8 --

''' 【简介】 QWebView中网页调用JavaScript

'''

from PyQt5.QtWidgets import QApplication , QWidget , QVBoxLayout , QPushButton from PyQt5.QtWebEngineWidgets import QWebEngineView import sys

创建一个 application实例

app = QApplication(sys.argv) win = QWidget() win.setWindowTitle('Web页面中的JavaScript与 QWebEngineView交互例子')

创建一个垂直布局器

layout = QVBoxLayout() win.setLayout(layout)

创建一个 QWebEngineView 对象

view = QWebEngineView() view.setHtml(''' <html> <head> <title>A Demo Page</title>

代码语言:javascript复制
  <script language="javascript">
    // Completes the full-name control and
    // shows the submit button
    function completeAndReturnName() {
      var fname = document.getElementById('fname').value;
      var lname = document.getElementById('lname').value;
      var full = fname   ' '   lname;

      document.getElementById('fullname').value = full;
      document.getElementById('submit-btn').style.display = 'block';

      return full;
    }
  </script>
</head>

<body>
  <form>
    <label for="fname">First name:</label>
    <input type="text" name="fname" id="fname"></input>
    <br />
    <label for="lname">Last name:</label>
    <input type="text" name="lname" id="lname"></input>
    <br />
    <label for="fullname">Full name:</label>
    <input disabled type="text" name="fullname" id="fullname"></input>
    <br />
    <input style="display: none;" type="submit" id="submit-btn"></input>
  </form>
</body>

</html> ''')

创建一个按钮去调用 JavaScript代码

button = QPushButton('设置全名')

def js_callback(result): print(result)

def complete_name(): view.page().runJavaScript('completeAndReturnName();', js_callback)

按钮连接 'complete_name'槽,当点击按钮是会触发信号

button.clicked.connect(complete_name)

把QWebView和button加载到layout布局中

layout.addWidget(view) layout.addWidget(button)

显示窗口和运行app

win.show() sys.exit(app.exec_())


-- coding: utf-8 --

''' 【简介】 QWebView中网页调用JavaScript

'''

from PyQt5.QtWidgets import QApplication , QWidget , QVBoxLayout from PyQt5.QtWebEngineWidgets import QWebEngineView from PyQt5.QtCore import QUrl from MySharedObject import MySharedObject from PyQt5.QtWebChannel import QWebChannel import sys

创建一个 application实例

app = QApplication(sys.argv) win = QWidget() win.setWindowTitle('Web页面中的JavaScript与 QWebEngineView交互例子')

创建一个垂直布局器

layout = QVBoxLayout() win.setLayout(layout)

创建一个 QWebEngineView 对象

view = QWebEngineView() htmlUrl = 'http://127.0.0.1:8020/web/index.html' view.load( QUrl( htmlUrl ))

创建一个 QWebChannel对象,用来传递pyqt参数到JavaScript

channel = QWebChannel( ) myObj = MySharedObject() channel.registerObject( "bridge", myObj ) view.page().setWebChannel(channel)

把QWebView和button加载到layout布局中

layout.addWidget(view)

显示窗口和运行app

win.show() sys.exit(app.exec_())


-- coding: utf-8 --

''' 【简介】 QWebEngineView加载网页,使网页中的用JavaScript 失效

'''

from PyQt5.QtWidgets import QApplication , QWidget , QVBoxLayout, QPushButton from PyQt5.QtWebEngineWidgets import QWebEngineView , QWebEngineSettings from PyQt5.QtCore import QUrl from MySharedObject import MySharedObject from PyQt5.QtWebChannel import QWebChannel from PyQt5.QtGui import QIcon import sys

class Web(QWebEngineView):

代码语言:javascript复制
def load(self, url):
    self.setUrl(QUrl(url))

def adjustTitle(self):
    #self.setWindowTitle(self.title())
    pass

def disableJS(self):
    settings = QWebEngineSettings.globalSettings()
    settings.setAttribute(QWebEngineSettings.JavascriptEnabled, False)

class Main(QWidget):

代码语言:javascript复制
def __init__(self):
    super().__init__()
    self.initUI()

def initUI(self):
    self.setWindowTitle('Name')
    self.setWindowIcon(QIcon('./images/cartoon1.ico'))  
    web = Web()                                         
    
    web.load("http://127.0.0.1:8020/web/index2.html")
    web.disableJS()
    
    self.btn = QPushButton('Button', self)
    self.btn.resize(self.btn.sizeHint())
    lay = QVBoxLayout(self)
    lay.addWidget(self.btn)
    lay.addWidget(web)

if name=="main": app = QApplication(sys.argv) main = Main() main.show() sys.exit(app.exec_())

0 人点赞