PyQt5相当于QtWebKitWidgets.QWebView.page.mainFrame(),用于QtWebEngineWidgets.QWebEngineView()?

时间:2016-11-11 16:43:09

标签: python pyqt pyqt5 qtwebengine

我是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版本工作?非常感谢帮助。

1 个答案:

答案 0 :(得分:3)

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

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

def js_callback(result):
    print(result)

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