簡體   English   中英

使用 PySide/PyQt 運行 QThread 時禁用元素

[英]Disable elements when running QThread with PySide/PyQt

這是我單擊“確定”按鈕時得到的。

但我想在run_action()仍在運行時禁用這兩個按鈕,最后將bar重置為 0。

這是我當前的代碼:

import sys
import time

from PySide6.QtCore import QThread, Signal
from PySide6.QtWidgets import (
    QApplication,
    QHBoxLayout,
    QProgressBar,
    QPushButton,
    QWidget,
)


class External(QThread):
    progressChanged = Signal(int)

    def run(self):
        progress = 0
        while progress < 100:
            progress += 10
            time.sleep(1)
            self.progressChanged.emit(progress)


class Window(QWidget):
    """The main application Window."""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Example")

        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(6, 6, 6, 6)

        self.bar = QProgressBar()
        self.bar.setTextVisible(False)
        self.bar.setValue(0)
        self.layout.addWidget(self.bar)

        self.cancel_btn = QPushButton("Cancel")
        self.cancel_btn.clicked.connect(self.close)
        self.layout.addWidget(self.cancel_btn)

        self.ok_btn = QPushButton("OK")
        self.ok_btn.clicked.connect(self.run_action)
        self.layout.addWidget(self.ok_btn)

        self.setLayout(self.layout)

    def run_action(self):
        self.ok_btn.setEnabled(False)
        self.cancel_btn.setEnabled(False)

        self.calc = External()
        self.calc.progressChanged.connect(self.onProgressChanged)
        self.calc.start()

        self.cancel_btn.setEnabled(True)
        self.ok_btn.setEnabled(True)
        self.bar.setValue(0)

    def onProgressChanged(self, value):
        self.bar.setValue(value)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

無效 QThread:: finished ()

該信號在完成執行之前從關聯線程發出。

當這個信號發出時,事件循環已經停止運行。 除了延遲刪除事件外,線程中不會再處理任何事件。 該信號可以連接到 QObject::deleteLater(),以釋放該線程中的對象。

import sys
#import time

#from PySide6.QtCore import QThread, Signal
from PyQt5.QtCore import QThread, pyqtSignal

#from PySide6.QtWidgets import (
from PyQt5.QtWidgets import (
    QApplication,
    QHBoxLayout,
    QProgressBar,
    QPushButton,
    QWidget,
)


class External(QThread):
#    progressChanged = Signal(int)
    progressChanged = pyqtSignal(int)

    def run(self):
        progress = 0
        while progress <= 100:
            self.progressChanged.emit(progress)
            self.msleep(500)
            progress += 10


class Window(QWidget):
    """The main application Window."""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Example")

        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(6, 6, 6, 6)

        self.bar = QProgressBar()
        self.bar.setTextVisible(False)
        self.bar.setValue(0)
        self.layout.addWidget(self.bar)

        self.cancel_btn = QPushButton("Cancel")
        self.cancel_btn.clicked.connect(self.close)
        self.layout.addWidget(self.cancel_btn)

        self.ok_btn = QPushButton("OK")
        self.ok_btn.clicked.connect(self.run_action)
        self.layout.addWidget(self.ok_btn)

        self.setLayout(self.layout)

    def run_action(self):
        self.ok_btn.setEnabled(False)        
        self.cancel_btn.setEnabled(False)        

        self.calc = External()
        self.calc.progressChanged.connect(self.onProgressChanged)
        self.calc.finished.connect(self.onFinished)                     # +++
        self.calc.start()

#        self.cancel_btn.setEnabled(True)
#        self.ok_btn.setEnabled(True)
#        self.bar.setValue(0)

    def onProgressChanged(self, value):
        self.bar.setValue(value)

    def onFinished(self):                                               # +++
        self.cancel_btn.setEnabled(True)
        self.ok_btn.setEnabled(True)
        self.bar.setValue(0)        

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM