打开具有不同参数的多个网页。 PyQt4
Posted
技术标签:
【中文标题】打开具有不同参数的多个网页。 PyQt4【英文标题】:Open multiple webpages with different parametrs. PyQt4 【发布时间】:2012-12-19 19:49:16 【问题描述】:我在Question 上关注了X.Jacobs 的答案,并试图创建一个应用程序,该应用程序将打开具有不同参数的多个窗口,但它不起作用,看起来,就像应用程序正在打开窗口,但是不加载网页。
#! /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】:该代码中发生了几件事 :) 查看此工作版本,看看如何使其适应您的:
#!/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_()
【讨论】:
太棒了!非常非常感谢你!你是最棒的以上是关于打开具有不同参数的多个网页。 PyQt4的主要内容,如果未能解决你的问题,请参考以下文章