gpt4 book ai didi

python - 如何更改pyqt5 qwebengineview中html的一部分

转载 作者:行者123 更新时间:2023-12-01 07:16:20 24 4
gpt4 key购买 nike

我已经在 qwebengineview.setHtml() 中查看了一些 html,现在我想在单击该 html View 中的按钮时运行一个 python 函数,然后将一些 html 添加到我的 html View 中,这是我的函数的输出我打过电话了。具体来说:我希望当按下表单按钮时我希望删除输入。

我读过这篇文章question但它没有告诉我如何在我的 html 中进行更改或更改其中的一部分或附加到它。
示例:

import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets, QtWebChannel
from jinja2 import Template


class Element(QtCore.QObject):
def __init__(self, name, parent=None):
super(Element, self).__init__(parent)
self._name = name

@property
def name(self):
return self._name

def script(self):
raise NotImplementedError


class FormObject(Element):
numbersChanged = QtCore.pyqtSignal(str, str)

def script(self):
_script = r"""
var btn = document.getElementById('sub1');
btn.addEventListener("click", function(event){
var number1 = document.getElementById('num1');
var number2 = document.getElementById('num2');
{{name}}.update(number1.value , number2.value);
});
"""
return Template(_script).render(name=self.name)

@QtCore.pyqtSlot(str, str)
def update(self, number1, number2):
self.numbersChanged.emit(number1, number2)


class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
def __init__(self, *args, **kwargs):
super(WebEnginePage, self).__init__(*args, **kwargs)
self.loadFinished.connect(self.onLoadFinished)
self._objects = []

def add_object(self, obj):
self._objects.append(obj)

@QtCore.pyqtSlot(bool)
def onLoadFinished(self, ok):
if ok:
self.load_qwebchannel()
self.load_objects()

def load_qwebchannel(self):
file = QtCore.QFile(":/qtwebchannel/qwebchannel.js")
if file.open(QtCore.QIODevice.ReadOnly):
content = file.readAll()
file.close()
self.runJavaScript(content.data().decode())
if self.webChannel() is None:
channel = QtWebChannel.QWebChannel(self)
self.setWebChannel(channel)

def load_objects(self):
if self.webChannel() is not None:
objects = {obj.name: obj for obj in self._objects}
self.webChannel().registerObjects(objects)
_script = r"""
{% for obj in objects %}
var {{obj}};
{% endfor %}
new QWebChannel(qt.webChannelTransport, function (channel) {
{% for obj in objects %}
{{obj}} = channel.objects.{{obj}};
{% endfor %}
});
"""
self.runJavaScript(Template(_script).render(objects=objects.keys()))
for obj in self._objects:
if isinstance(obj, Element):
self.runJavaScript(obj.script())


class WebPage(QtWebEngineWidgets.QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)

page = WebEnginePage(self)
self.setPage(page)

formobject = FormObject("formobject", self)
formobject.numbersChanged.connect(self.on_numbersChanged)
page.add_object(formobject)

filepath = os.path.abspath(
os.path.join(os.path.dirname(__file__), "new.html")
)
self.load(QtCore.QUrl.fromLocalFile(filepath))

@QtCore.pyqtSlot(str, str)
def on_numbersChanged(self, number1, number2):
print(number1, number2)


if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
web = WebPage()
web.show()
sys.exit(app.exec_())

<!DOCTYPE <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>

最佳答案

如果你想从 HTML 中删除一个元素,你必须使用 js 来完成,但主要问题是知道何时执行此操作,在表单的情况下,页面会重新加载,因此你必须立即删除“输入”加载后,可以使用带有适当“t”的 QTimer.singleShot (t, func)。

考虑到上述情况并使用相同的代码,那么解决方案是

class WebPage(QtWebEngineWidgets.QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)

page = WebEnginePage(self)
self.setPage(page)

formobject = FormObject("formobject", self)
formobject.numbersChanged.connect(self.on_numbersChanged)
page.add_object(formobject)

filepath = os.path.abspath(
os.path.join(os.path.dirname(__file__), "new.html")
)
self.load(QtCore.QUrl.fromLocalFile(filepath))

@QtCore.pyqtSlot(str, str)
def on_numbersChanged(self, number1, number2):
print(number1, number2)
<b>QtCore.QTimer.singleShot(100, self.remove_inputs)

def remove_inputs(self):
self.page().runJavaScript(
"""
function remove_by_id(_id){
var elem = document.getElementById(_id);
elem.parentNode.removeChild(elem);
}
remove_by_id("num1");
remove_by_id("num2");
"""
)</b>

关于python - 如何更改pyqt5 qwebengineview中html的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57926683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com