4

我正在尝试获得一个无边框的 python PyQt webkit 窗口来显示单个网站 html 表单。单击发送时,表单值应保存在字典中并关闭窗口。

到目前为止(在SO的帮助下)我有无边界窗口并且可以获取输入。但是,缺少两件事:

  1. 按下发送后关闭窗口。
  2. 获取字典中的输入elements(注意 的键elements对应于 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。

4

1 回答 1

3

要将表单数据放入 adict中,最好使用unquote_pluspython 标准库,因为(与 不同QUrl)它可以处理加号和百分比编码。

formSubmitted要关闭窗口,您可以从网页发出信号,并将其连接到主窗口上的处理程序。然后这个处理程序可以调用close()主窗口,对表单数据进行所有处理,最后quit()是应用程序。

要删除页面周围的边框,请将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()
于 2012-09-03T00:15:30.423 回答