Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19529

Re: calling a simple PyQt application more than once

Date 2012-01-27 18:42 -0500
From John Posner <jjposner@optimum.net>
Subject Re: calling a simple PyQt application more than once
References <CAOuJsMmyDArFXXCbNFPCiXPytPBOg8X5DHXRHxot8dmrwrwUjg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5161.1327709549.27778.python-list@python.org> (permalink)

Show all headers | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: calling a simple PyQt application more than once John Posner <jjposner@optimum.net> - 2012-01-27 18:42 -0500

csiph-web