Open multiple webpages with different parametrs. P

2019-08-06 13:14发布

I have followed the answer by X.Jacobs on this Question, and was trying to create an app that will open multiple windows with different parameters, but it doesn't work, looks, like app is opening windows, but not load the webpage.

#! /usr/bin/env python2.7

from PyQt4.QtCore import *
from PyQt4.QtGui import*
from PyQt4.QtWebKit import *
import sys, signal

url = 'http://http://forums.fedoraforum.org/showthread.php?t={0}'

class Opener(QWebView):
    def __init__(self, param=None):
        QWebView.__init__(self)
        self.param = param
        self.loadFinished.connect(self.print_title)

    def print_title(self):
        print self.title()

class Foo(QObject):
    def __init__(self):
        QObject.__init__(self)
        self.count = 0
        self.params = range(4)
        self.mapper = QSignalMapper(self)
        self.mapper.mapped.connect(self.mapper_mapped)

        for i in range(2):
            opener = Opener()
            opener.loadFinished.connect(self.mapper.map)
            self.mapper.setMapping(opener, i)
            opener.loadFinished.emit(True)
            QTimer.singleShot(1, lambda:opener.loadFinished.emit(True))  

    def mapper_mapped(self, gNumber):
        self.count += 1
        if self.count < len(self.params):
            gParam  = self.params[self.count]
            opener = self.mapper.mapping(gNumber)
            opener.load(QUrl(url.format(gParam)))
            opener.show()
            QTimer.singleShot(1, lambda:opener.loadFinished.emit(True)) 

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = Foo()
    if signal.signal(signal.SIGINT, signal.SIG_DFL):
        sys.exit(app.exec_()) 
    app_exec_()

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-06 13:43

There's a couple of things going on in that code :) checkout this working version and see how you can adapt it to yours:

#!/usr/bin/env python

from PyQt4.QtCore import *
from PyQt4.QtGui import*
from PyQt4.QtWebKit import *

class Foo(QWidget):
    def __init__(self, parent=None):
        super(Foo, self).__init__(parent)    

        self.count  = 0
        self.params = range(4)
        self.url    = 'http://forums.fedoraforum.org/showthread.php?t={0}'

        self.gridLayout = QGridLayout(self)
        self.tabWidget = QTabWidget(self)
        self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)

        self.mapper = QSignalMapper(self)
        self.mapper.mapped.connect(self.on_mapper_mapped)

        for i in range(2):
            grabber = QWebView()
            grabber.loadFinished.connect(self.mapper.map)

            self.mapper.setMapping(grabber, i)
            self.tabWidget.addTab(grabber, "opener {0}".format(str(i)))

            grabber.loadFinished.emit(True)

    @pyqtSlot(int)
    def on_mapper_mapped(self, gNumber):
        self.count += 1
        if self.count < len(self.params):
            gParam = self.params[self.count]

            opener = self.mapper.mapping(gNumber)
            opener.load(QUrl(self.url.format(gParam)))

if __name__ == "__main__":
    import  sys

    app = QApplication(sys.argv)
    main = Foo()
    main.show()
    app.exec_()
查看更多
登录 后发表回答