栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PyQt,QThread,GIL,GUI

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PyQt,QThread,GIL,GUI

我认为您在使用装饰器方面过于复杂。您可以使用大约3-4行设置代码轻松地将代码包装在新线程中。另外,我不认为您应该直接从另一个线程调用完成的插槽。您应该使用连接的信号将其激活。

import sysfrom time import sleepfrom PyQt5.QtCore import *from PyQt5.QtWidgets import *class Signals(QObject):    update = pyqtSignal(int)    enable_button = pyqtSignal(bool)class Window(QWidget):    def __init__(self, *args, **kwargs):        QWidget.__init__(self, *args, **kwargs)        self.button = QPushButton("Run", self)        self.button.clicked.connect(self.onButton)        self.progress = QProgressBar(self)        self.progress.setTextVisible(False)        self.layout = QVBoxLayout()        self.layout.setContentsMargins(5, 5, 5, 5)        self.layout.addWidget(self.button)        self.layout.addWidget(self.progress)        self.layout.addStretch()        self.worker_thread = QThread()        self.worker_thread.run = self.worker        self.worker_thread.should_close = False        self.signals = Signals()        self.signals.update.connect(self.progress.setValue)        self.signals.enable_button.connect(self.button.setEnabled)        self.setLayout(self.layout)        self.show()        self.resize(self.size().width(), 0)    # Override    def closeEvent(self, e):        self.worker_thread.should_close = True        self.worker_thread.wait()    @pyqtSlot()    def onButton(self):        self.button.setDisabled(True)        self.worker_thread.start()    # Worker thread, no direct GUI updates!    def worker(self):        for i in range(101): if self.worker_thread.should_close:     break self.signals.update.emit(i) sleep(0.1)        self.signals.enable_button.emit(True)app = QApplication(sys.argv)win = Window()sys.exit(app.exec_())


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625576.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号