python - 如何在pyqt pyside中同时运行对象的多个实例

标签 python pyqt pyside qthread

在下面的代码中是一个示例程序,每当您单击“新建”按钮时,您都可以打开新选项卡,并且每个选项卡包含 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_())

最佳答案

您没有为 QThread 分配任何工作(不要重新实现 run 方法)。您已与 th.started 信号连接的 browser.load 插槽在主线程中执行(因为您的浏览器是在其中创建的)。这会导致您的问题。

此外,您只能从主线程对 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()

关于python - 如何在pyqt pyside中同时运行对象的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31680352/

相关文章:

python - 在 python : extrapolate background color 中旋转图像

python - 测试 ndb 实体是否已放入数据存储

python - PyQt5 - 滚动到 QTextEdit 的光标

python - 更改 QSpinBox 值时调用函数

qt - 是否可以设置 QTableView 角按钮的文本?

python - PySide-QtWebKit : CSS font-family has no effect

python - 四舍五入 ** 0.5 和 math.sqrt

python - Odoo 13 - 搜索方法 - 使用 'order' 属性作为模型的相关字段

python - 小部件未显示在网格布局 (PySide) 上

python - Pyside uiLoader 捕获关闭事件信号