我有一个PyQt4类,它显示一个html表单并获取输入,这要归功于之前的questions here(older one is here}。
现在我想退出主循环,执行其他操作并稍后使用不同的html表单重新启动主循环。问题是,我的事件句柄在主循环的第二次执行中不起作用。我做错了什么?
问题似乎是,当第二次调用MyWebPage
时,类app.exec_()
无法正常工作,因为表单未提交。
这是我运行的代码,但不提交第二种形式:
import sys
from urllib import unquote_plus
from PyQt4 import QtCore, QtGui, QtWebKit
class MyWebPage(QtWebKit.QWebPage):
formSubmitted = QtCore.pyqtSignal(QtCore.QUrl)
def acceptNavigationRequest(self, frame, req, nav_type):
if nav_type == QtWebKit.QWebPage.NavigationTypeFormSubmitted:
self.formSubmitted.emit(req.url())
return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type)
class PsyTML(QtGui.QWidget):
def __init__(self):
super(PsyTML, self).__init__()
self.elements = {}
self.view = QtWebKit.QWebView(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.view)
layout.setContentsMargins(0, 0, 0, 0)
self.view.setPage(MyWebPage())
self.view.page().formSubmitted.connect(self.handleFormSubmitted)
def viewPsyTML(self, html):
self.view.setHtml(html)
self.show()
def handleFormSubmitted(self, url):
elements = self.elements
self.close()
for key, value in url.encodedQueryItems():
key = unquote_plus(bytes(key)).decode('utf8')
value = unquote_plus(bytes(value)).decode('utf8')
elements[key] = value
# do stuff with elements...
for item in elements.iteritems():
print '"%s" = "%s"' % item
QtGui.qApp.quit()
# setup the html form
html = """
<form action="" method="get">
Like it?
<input type="radio" name="like" value="yes"/> Yes
<input type="radio" name="like" value="no" /> No
<br/><input type="text" name="text" value="" />
<input type="submit" name="submit" value="Send"/>
</form>
"""
html2 = """
<form action="" method="get">
Choose wisely?
<input type="radio" name="choose" value="A"/> Yes
<input type="radio" name="choose" value="B" /> No
<br/><input type="text" name="text" value="" />
<input type="submit" name="submit" value="Send"/>
</form>
"""
def main():
app = QtGui.QApplication(sys.argv)
intro = PsyTML()
intro.viewPsyTML(html)
# now, the html form is displayed nicely and the form elemtns returned
app.exec_()
# do something else here
# in the secomd run, the form is not submitted.
intro.viewPsyTML(html2)
app.exec_()
if __name__ == "__main__":
main()
我正在使用Python 2。
答案 0 :(得分:1)
需要更改程序的当前结构。
首先,main
函数应尽可能简单。它应该只创建一个应用程序对象和一个主窗口,然后运行事件循环。在那之后,一切都应该围绕信号/事件及其处理程序构建。
目前,只有一个中心事件:表单提交。这发生异步,因此应用程序必须保存其当前状态,然后被动地等待每个表单完成,然后才能继续执行下一个任务。
修改强>
既然您的规范更清晰,实现目标的一种方法是使用QDialog
,它有自己的事件循环。
以下是您的脚本的修订版本,希望更接近您想要的内容:
import sys
from urllib import unquote_plus
from PyQt4 import QtCore, QtGui, QtWebKit
class MyWebPage(QtWebKit.QWebPage):
formSubmitted = QtCore.pyqtSignal(object)
def acceptNavigationRequest(self, frame, req, nav_type):
if nav_type == QtWebKit.QWebPage.NavigationTypeFormSubmitted:
elements = {}
for key, value in req.url().encodedQueryItems():
key = unquote_plus(bytes(key)).decode('utf8')
value = unquote_plus(bytes(value)).decode('utf8')
elements[key] = value
self.formSubmitted.emit(elements)
return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type)
class PsyTML(QtGui.QDialog):
def __init__(self):
super(PsyTML, self).__init__()
self.elements = {}
self.view = QtWebKit.QWebView(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.view)
layout.setContentsMargins(0, 0, 0, 0)
self.view.setPage(MyWebPage())
self.view.page().formSubmitted.connect(self.handleFormSubmitted)
def viewPsyTML(self, html):
self.view.setHtml(html)
self.exec_()
def handleFormSubmitted(self, elements):
self.elements = elements
self.accept()
# setup the html form
html = """
<form action="" method="get">
Like it?
<input type="radio" name="like" value="yes"/> Yes
<input type="radio" name="like" value="no" /> No
<br/><input type="text" name="text" value="" />
<input type="submit" name="submit" value="Send"/>
</form>
"""
html2 = """
<form action="" method="get">
Choose wisely?
<input type="radio" name="choose" value="A"/> Yes
<input type="radio" name="choose" value="B" /> No
<br/><input type="text" name="text" value="" />
<input type="submit" name="submit" value="Send"/>
</form>
"""
def main():
app = QtGui.QApplication(sys.argv)
intro = PsyTML()
intro.viewPsyTML(html)
# do stuff with elements...
for item in intro.elements.iteritems():
print '"%s" = "%s"' % item
intro = PsyTML()
intro.viewPsyTML(html2)
if __name__ == "__main__":
main()