PyQt Webkit and HTML Forms: Selecting an Output and Close Window

I am trying to get a limitless python PyQt webkit window to display a single HTML site form. When you click on submit, the form values ​​must be saved in the dictionary, and the window is closed.

So far (with the help of https://stackoverflow.com/a/3/3/) ... I have a borderless window and you can get input. However, two things are missing:

  • Closing the window after clicking send.
  • Getting input in the elements dictionary (note that the keys correspond to the names of the html forms).

(maybe, on the contrary, it will be better, but 1 seems more complicated)

My code so far:

 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() 

The ideal answer will not only show how to (a) save the input and (b) close the window , but (c) also remove the remaining small gray border around the html page .

Update : I am using Python 2.

+4
source share
1 answer

To get the form data in a dict , it is better to use unquote_plus from the python standard library as (unlike QUrl ), it can handle plus signs as well as percent encoding.

To close the window, you can emit the formSubmitted signal from the web page and connect it to the handler in the main window. This handler can then call close() in the main window, do all the processing of the form data, and then finally the quit() application.

To remove the border around the page, set the contentsMargins main layout to zero.

Here is a revised version of your script that implements the following ideas:

 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() 
+2
source

All Articles