Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19529 > unrolled thread
| Started by | John Posner <jjposner@optimum.net> |
|---|---|
| First post | 2012-01-27 18:42 -0500 |
| Last post | 2012-01-27 18:42 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: calling a simple PyQt application more than once John Posner <jjposner@optimum.net> - 2012-01-27 18:42 -0500
| From | John Posner <jjposner@optimum.net> |
|---|---|
| Date | 2012-01-27 18:42 -0500 |
| Subject | Re: calling a simple PyQt application more than once |
| Message-ID | <mailman.5161.1327709549.27778.python-list@python.org> |
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
Back to top | Article view | comp.lang.python
csiph-web