【发布时间】:2019-09-20 16:28:43
【问题描述】:
我有一个form.html 文件,我使用 PyQt5 QWebEngineView 运行它。
单击提交按钮后,我想检索表单值。我经历了许多解决方案,但找不到完美的解决方案。
例如:我找到了this one,但它适用于URL Change。
我只是在寻找一个可以点击提交按钮的解决方案。
这是我从上述链接中使用的代码,但我没有从表单返回任何值:
import os
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
class WebPage(QWebEngineView):
def __init__(self):
QWebEngineView.__init__(self)
filepath = os.path.abspath(os.path.join(os.path.dirname(__file__), 'form.html'))
self.load(QUrl.fromLocalFile(filepath))
self.loadFinished.connect(self._on_load_finished)
def _on_load_finished(self):
self.page().runJavaScript("document.getElementById('num1').value", self.store_value)
def store_value(self, param):
self.value = param
print("Param: " +str(param))
if __name__ == "__main__":
app = QApplication(sys.argv)
web = WebPage()
web.show()
sys.exit(app.exec_())
HTML:
<html>
<body>
<form>
Number 1:<input type="text" id="num1">
<br>
Number 2:<input type="text" id="num2">
<br>
<input type="submit" id="sub1">
</form>
</body>
</html>
【问题讨论】:
标签: python python-3.x pyqt pyqt5 qwebengineview