How do I display a website with html-forms locally using python and collect the user input?

后端 未结 5 1590
猫巷女王i
猫巷女王i 2021-01-06 19:17

I am a behavorial scientist and usually collect data by letting participants do some tasks on a computer and record their responses (I write the programs using the pyglet wr

5条回答
  •  醉酒成梦
    2021-01-06 19:25

    This is a solution using Qt Webkit for rendering HTML. The default navigation request handler is wrapped by a function that checks for submitted form requests. The form uses the "get" method, so the data is included in the url of the request and can be retrieved that way. The original request is declined and you can change the content of the displayed web page as you wish.

    from PyQt4 import QtGui, QtWebKit
    
    app = QtGui.QApplication([])
    view = QtWebKit.QWebView()
    
    # intercept form submits
    class MyWebPage(QtWebKit.QWebPage):
        def acceptNavigationRequest(self, frame, req, nav_type):
            if nav_type == QtWebKit.QWebPage.NavigationTypeFormSubmitted:
                text = "
    \n".join(["%s: %s" % pair for pair in req.url().queryItems()]) view.setHtml(text) return False else: return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type) view.setPage(MyWebPage()) # setup the html form html = """
    Like it? Yes No
    """ view.setHtml(html) # run the application view.show() app.exec_()

提交回复
热议问题