calling a simple PyQt application more than once

John Posner jjposner at
Fri Jan 27 18:42:24 EST 2012

Jabba Laci wrote:
> Hi,
> I have a simple PyQt application that creates a webkit instance to
> scrape AJAX web pages. It works well but I can't call it twice. I
> think the application is not closed correctly, that's why the 2nd call
> fails. Here is the code below. I also put it on pastebin:
> .
> The question is: how to call this code several times within a script.

You want to create/execute/quit a QApplication just once, not multiple

I don't know either WebKit  or AJAX. In fact, I've never done any
networking with PyQt. But a little playing with the QtNetwork module
yielded this, using the QNetworkAccessManager and QNetworkRequest classes:

from PyQt4.QtCore import QUrl
from PyQt4.QtGui import QApplication, QPushButton
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest

def process_page(reply_obj):
    resp = reply_obj.readAll()
    print str(resp).strip()

def do_click():
    req = QNetworkRequest(QUrl(MYURL))

MYURL = ''

if __name__ == "__main__":
    # we need only one application object and one net-access mgr
    app = QApplication([])
    mgr = QNetworkAccessManager()

    # the entire GUI is one button
    btn = QPushButton("Press me")

    # start the event loop

You can click the "Press me" button as many times as you wish; it
retrieves and displays/prints the same HTML file on each click.


More information about the Python-list mailing list