深度阅读

"pyqt5界面防卡死"

作者
作者
2023年08月22日
更新时间
10.38 分钟
阅读时间
0
阅读量

PyQt 的 QThread

import sysfrom PyQt5.QtCore import QThread, pyqtSignalfrom PyQt5.QtWidgets import QPushButton, QDialog, QApplicationclass BigThingThread(QThread):    finished_signal = pyqtSignal(str)    def __init__(self, rest, parent=None):        super().__init__(parent)        self._rest = rest    def run(self):        print('do something big')        time.sleep(self._rest)        self.finished_signal.emit('done')class MyDialog(QDialog):    def __init__(self, parent=None):        super().__init__(parent)        self.button = QPushButton(self)        self.button.setText('干大事')        self.button.clicked.connect(self._click_do_something)    @staticmethod    def _show_message(message):        print('{}'.format(message))    def _click_do_something(self):        self.big_thread = BigThingThread(1)        self.big_thread.finished_signal.connect(self._show_message)        self.big_thread.start()if __name__ == '__main__':    app = QApplication(sys.argv)    dialog = MyDialog()    dialog.show()    sys.exit(app.exec_())

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。