在下面的代码中一个示例程序,您可以在单击&#34时打开新选项卡;新"按钮和每个选项卡包含QWebView浏览器(在PyQt / PySide中使用)和"开始"按钮,当"开始"按钮单击浏览器逐个加载3个站点 问题是: 当打开多个选项卡并运行所有选项卡或多个选项卡时,所有其他选项卡将暂停加载,直到最后一个选项卡完成加载,然后下一个恢复 我想同时运行它们怎么样?
import sys
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtWebKit import *
class Main_Gui(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
self.bt_new = QPushButton('New')
self.tabwidget = QTabWidget()
layout.addWidget(self.tabwidget)
layout.addWidget(self.bt_new)
self.setLayout(layout)
self.bt_new.clicked.connect(self.add_new)
def add_new(self):
br = browser()
self.tabwidget.addTab(br, 'browser')
class browser(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QVBoxLayout()
self.br = QWebView()
self.bt_Start = QPushButton('Start')
layout.addWidget(self.br)
layout.addWidget(self.bt_Start)
self.setLayout(layout)
self.bt_Start.clicked.connect(self.start_load)
def start_load(self):
self.th = QThread()
self.th.started.connect(self.load)
self.th.start()
def load(self):
sites = ['https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/'
, 'http://www.codecademy.com/tracks/python', 'http://www.google.com']
for i in sites:
self.br.load(QUrl(i))
self.sleep(20)
def sleep(self, seconds):
end = QTime.addSecs(QTime.currentTime(), seconds)
while end > QTime.currentTime():
QCoreApplication.processEvents()
app = QApplication(sys.argv)
win = Main_Gui()
win.resize(800, 600)
win.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
您没有为QThread
分配任何工作(不要重新实现run
方法)。您已与browser.load
信号相关联的th.started
广告位在主广告代码中执行(因为您的浏览器是在其中创建的)。这会导致你的问题。
此外,您只能从主线程(为了线程安全)更改GUI(例如调用self.br.load
方法)。
class TimerThread(QThread):
nextURL = pyqtSignal(str)
def run(self):
sites = ['https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/',
'http://www.codecademy.com/tracks/python', 'http://www.google.com']
for url in sites:
print(self.thread(), url)
self.nextURL.emit(url)
self.sleep(10)
class browser(QWidget):
def __init__(self):
[...]
self.th = QThread()
def start_load(self):
self.stop_thread(self.th)
self.th = TimerThread()
self.th.nextURL.connect(self.load_url_slot)
self.th.start()
def load_url_slot(self, url):
self.br.load(QUrl(url))
def stop_thread(self, thread):
thread.terminate()
if thread.isRunning():
QCoreApplication.processEvents()
答案 1 :(得分:0)
感谢Alexander,他给了我线索,以及我在问题中提交的示例的完整代码,问题是我想告诉QThread要等到页面加载,我找到了一个解决方案,使一个名为loaded的局部变量在QThread中连接浏览器loadFinished信号到函数使加载变量为true时加载完成,在Qthread中我做了等待代码,直到加载var变为true
import sys
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtWebKit import *
from Browser_AutoPilot import BrowserAutoPilot
class Main_Gui(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
self.bt_new = QPushButton('New')
self.tabwidget = QTabWidget()
layout.addWidget(self.tabwidget)
layout.addWidget(self.bt_new)
self.setLayout(layout)
self.bt_new.clicked.connect(self.add_new)
def add_new(self):
br = browser()
self.tabwidget.addTab(br, 'browser')
class browser(QWidget):
load_finished = Signal()
def __init__(self):
QWidget.__init__(self)
layout = QVBoxLayout()
self.br = BrowserAutoPilot()
self.bt_Start = QPushButton('Start')
layout.addWidget(self.br)
layout.addWidget(self.bt_Start)
self.setLayout(layout)
self.th = TimerThread()
self.bt_Start.clicked.connect(self.start_load)
def start_load(self):
self.stop_thread(self.th)
self.th.nextURL.connect(self.load_url_slot)
self.br.loadFinished.connect(self.wait_loading)
self.th.start()
def load_url_slot(self, url):
self.br.load(QUrl(url))
def wait_loading(self):
self.th.loaded = True
def stop_thread(self, thread):
thread.terminate()
if thread.isRunning():
QCoreApplication.processEvents()
def sleep(self, seconds):
end = QTime.addSecs(QTime.currentTime(), seconds)
while end > QTime.currentTime():
QCoreApplication.processEvents()
class TimerThread(QThread):
nextURL = Signal(str)
def __init__(self, loaded=False):
QThread.__init__(self)
self.loaded = loaded
def run(self):
sites = ['https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/',
'http://www.codecademy.com/tracks/python', 'http://www.google.com']
for url in sites:
print(self.thread(), url)
self.nextURL.emit(url)
self.loaded = False
while not self.loaded:
self.sleep(1)
app = QApplication(sys.argv)
win = Main_Gui()
win.resize(800, 600)
win.show()
sys.exit(app.exec_())