我试图获得一个无边框
python PyQt webkit窗口来显示单个网站html表单.单击“发送”时,表单值应保存在字典中并关闭窗口.
到目前为止(在SO的帮助下)我有无边框窗口并且可以获取输入.但是,缺少两件事:
>按发送后关闭窗口.
>获取字典元素中的输入(请注意,元素的键对应于html表单名称).
(可能反过来会更好,但1似乎更难)
到目前为止我的代码是:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
elements = {"like":"", "text": ""}
class MyWebPage(QWebPage):
def acceptNavigationRequest(self, frame, req, nav_type):
if nav_type == QWebPage.NavigationTypeFormSubmitted:
text = "<br/>\n".join(["%s: %s" % pair for pair in req.url().queryItems()])
print(text)
return True
else:
return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type)
class Window(QWidget):
def __init__(self, html):
super(Window, self).__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
view = QWebView(self)
layout = QVBoxLayout(self)
layout.addWidget(view)
view.setPage(MyWebPage())
view.setHtml(html)
# 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="Hello" />
<input type="submit" name="submit" value="Send"/>
</form>
"""
def main():
app = QApplication(sys.argv)
window = Window(html)
window.show()
app.exec_()
if __name__ == "__main__":
main()
一个完美的答案不仅会显示如何(a)保存输入和(b)关闭窗口,但(c)还删除html页面周围剩余的小灰色边框.
更新:我使用Python 2.
最佳答案 要将表单数据放入dict中,最好使用python标准库中的
unquote_plus
(与QUrl不同)它可以处理加号和百分比编码.
要关闭窗口,您可以从Web页面发出formSubmitted信号,并将其连接到主窗口上的处理程序.然后,此处理程序可以在主窗口上调用close(),对表单数据执行所有处理,然后最终退出()应用程序.
要删除页面周围的边框,请将主布局的contentsMargins
设置为零.
以下是您的脚本的修订版本,它实现了上述想法:
import sys
from urllib import unquote_plus
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
class MyWebPage(QWebPage):
formSubmitted = pyqtSignal(QUrl)
def acceptNavigationRequest(self, frame, req, nav_type):
if nav_type == QWebPage.NavigationTypeFormSubmitted:
self.formSubmitted.emit(req.url())
return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type)
class Window(QWidget):
def __init__(self, html):
super(Window, self).__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
view = QWebView(self)
layout = QVBoxLayout(self)
layout.addWidget(view)
layout.setContentsMargins(0, 0, 0, 0)
view.setPage(MyWebPage())
view.setHtml(html)
view.page().formSubmitted.connect(self.handleFormSubmitted)
def handleFormSubmitted(self, url):
self.close()
elements = {}
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
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>
"""
def main():
app = QApplication(sys.argv)
window = Window(html)
window.show()
app.exec_()
if __name__ == "__main__":
main()