【问题标题】:PyQt5 equivalent of QtWebKitWidgets.QWebView.page.mainFrame() for QtWebEngineWidgets.QWebEngineView()?PyQt5 等效于 QtWebEngineWidgets.QWebEngineView() 的 QtWebKitWidgets.QWebView.page.mainFrame()?
【发布时间】:2016-11-11 16:43:08
【问题描述】:

我是 PyQt 的新手,开始玩弄以下代码(最初来自 this blog post):

# Create an application
app = QApplication([])

# And a window
win = QWidget()
win.setWindowTitle('QWebView Interactive Demo')

# And give it a layout
layout = QVBoxLayout()
win.setLayout(layout)

# Create and fill a QWebView
view = QWebView()
view.setHtml('''
  <html>
    <head>
      <title>A Demo Page</title>

      <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>
''')

# A button to call our JavaScript
button = QPushButton('Set Full Name')

# Interact with the HTML page by calling the completeAndReturnName
# function; print its return value to the console
def complete_name():
    frame = view.page().mainFrame()
    print frame.evaluateJavaScript('completeAndReturnName();')

# Connect 'complete_name' to the button's 'clicked' signal
button.clicked.connect(complete_name)

# Add the QWebView and button to the layout
layout.addWidget(view)
layout.addWidget(button)

# Show the window and run the app
win.show()
app.exec_()

我做了一些小改动,试图让它在最新的 pyqt5 版本上运行,但我不明白我应该如何改变

frame = view.page().mainFrame()

为了使脚本运行无误。这是我到目前为止的代码:

from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5 import QtWebEngineWidgets

# Create an application
app = QtWidgets.QApplication([])

# And a window5
win = QtWidgets.QWidget()
win.setWindowTitle('QWebView Interactive Demo')

# And give it a layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)

# Create and fill a QWebView
#view = QtWebKitWidgets.QWebView()  # depecated?
view = QtWebEngineWidgets.QWebEngineView()
view.setHtml('''
  <html>
    <head>
      <title>A Demo Page</title>

      <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>
''')

# A button to call our JavaScript
button = QtWidgets.QPushButton('Set Full Name')

# Interact with the HTML page by calling the completeAndReturnName
# function; print its return value to the console
def complete_name():
    frame = view.page().mainFrame()  # THIS raises an error. I'm stuck here.

    print(frame.evaluateJavaScript('completeAndReturnName();'))

# Connect 'complete_name' to the button's 'clicked' signal
button.clicked.connect(complete_name)

# Add the QWebView and button to the layout
layout.addWidget(view)
layout.addWidget(button)

# Show the window and run the app
win.show()
app.exec_()

我见过this post,我认为这可能会有所帮助,但不幸的是我仍然坚持下去。有谁知道如何使用最新的 PyQt5 版本进行这项工作?非常感谢您的帮助。

【问题讨论】:

    标签: python pyqt pyqt5 qtwebengine


    【解决方案1】:

    在 Qt Web 引擎中没有与 Qt WebKit QWebFrame 类等效的东西。框架仅被视为内容的一部分,因此没有专门的 API 来处理它们 - 只有一个 QWebEnginePage,它提供对整个 Web 文档的访问。

    也没有evaluateJavaScript 方法。相反,有一个异步的runJavaScript 方法,它需要一个回调来接收结果。所以你的代码应该像这样重写:

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

    【讨论】:

    • 非常感谢您的解释和解决方案,现在可以了!
    猜你喜欢
    • 2018-08-29
    • 2019-11-01
    • 2020-06-08
    • 2022-07-05
    • 2019-06-29
    • 2021-09-22
    • 1970-01-01
    • 2012-02-18
    • 2017-06-20
    相关资源
    最近更新 更多