简体   繁体   中英

How to return value after loadFinished in PyQt4

I have a script, that works fine:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    bot = GBot()
    bot.search('hot tea', num=20)
    if signal.signal(signal.SIGINT, signal.SIG_DFL):
        sys.exit(app.exec_())
    app.exec_()

When I call search(), program starts working, and and loads website:

def _loadFinished(self, ok):
        current_url = self.page().currentFrame().url().toString()
        if str(current_url).endswith('.com/'):
            self.home_search()
        else:
            self.get_links_text_from_page()

        if self.count >= self.desired_number_of_results:
            self.close()

After load finished 1 time, it checks for another condition and desides what to do next. At the end, after program loads multiple websites. Desired data collected in variable called self.results .

So my question is how I can return result from search(), by checking condition of loadFinished().

Another words, I need to come up with some sort of algorithm that will check if loadFinished will not load any other websites, and than search() function will return desired variable. I was thinking to create another variable self.result = False than change the condition in loadFinished() and in search() place everything in while loop , and after that return result. But it doesn't work...

search()

def search(self, keyword, num=None, output=None):
    self.keyword = keyword
    if output is "json":
    # need to return `self.results` ONLY after program finished. because before that,
    # this variable is empty
    self.load('somewebsite.com')
    pass

Looks like you could use a generator here. In this QWebView example, loadWebsites is called until StopIteration is raised, in which case procDone is emitted with the number of loaded websites. The output for that signal is captured in the slot on_procDone . (The output in this case is 3 , because of ["http://www.example.com"]*3 ):

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork

class myWindow(QtWebKit.QWebView):
    procDone = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        self.websites      = iter(["http://www.example.com"]*3)
        self.websitesTotal = 0

        self.loadFinished.connect(self.on_loadFinished)
        self.procDone.connect(self.on_procDone)

        self.loadWebsites()

    def loadWebsites(self):
        try:
            website = self.websites.next()

        except StopIteration:
            self.procDone.emit(self.websitesTotal)

        else:
            self.load(QtCore.QUrl(website))

    @QtCore.pyqtSlot(bool)
    def on_loadFinished(self, ok):
        self.websitesTotal += 1
        print "Loaded: {0}".format(self.url().toString())
        self.loadWebsites()

    @QtCore.pyqtSlot(int)
    def on_procDone(self, total):
        print "Total of websites: {0}".format(total)
        self.websitesTotal = 0

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myWindow')

    main = myWindow()
    main.show()

    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM