calling a simple PyQt application more than once
John Posner
jjposner at optimum.net
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:
> http://pastebin.com/gkgSSJHY .
>
> 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
times.
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()
reply_obj.close()
print str(resp).strip()
def do_click():
req = QNetworkRequest(QUrl(MYURL))
mgr.finished.connect(process_page)
mgr.get(req)
MYURL = 'http://simile.mit.edu/crowbar/test.html'
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")
btn.clicked.connect(do_click)
btn.show()
# start the event loop
app.exec_()
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.
HTH,
John
More information about the Python-list
mailing list