I am trying to get a borderless python PyQt webkit window to display a single website html form. When clicking on send, the form values should be saved in a dictionnary and the window closed.
So far (with the help of a SO) I have the borderless window and can fetch the input. However, two things are missing:
- Closing the window after pressing send.
- Fetching the input in the dictionary
elements(note that the keys ofelementscorrespond to the html form names).
(potentially the other way round would be better, but 1 seems more difficult)
My code so far is:
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 perfect answer would not only show how to (a) save the input and (b) close the window, but (c) also remove the remaining small grey border around the html page.
Update: I use Python 2.
To get the form data into a
dict, it is best to useunquote_plusfrom the python standard library as (unlikeQUrl) it can handle plus-signs as well as percent-encoding.To close the window, you could emit a
formSubmittedsignal from the web-page, and connect it to a handler on the main-window. This handler could then callclose()on the main-window, do all the processing of the form data, and then finallyquit()the application.To remove the border around the page, set the
contentsMarginsof the main layout to zero.Here is revised version of your script which implements the above ideas: