Untitled
unknown
python
2 years ago
2.0 kB
15
Indexable
import sys
import time
from PyQt6.QtCore import Qt, QObject, QThread, pyqtSignal
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel
class Worker(QObject):
finished = pyqtSignal()
progress = pyqtSignal(int)
def run(self):
for i in range(1, 101):
time.sleep(0.1)
self.progress.emit(i)
self.finished.emit()
class MyWidget(QWidget):
def __init__(self):
super().__init__()
# Création du layout vertical pour la fenêtre
layout = QVBoxLayout()
# Ajout d'un label pour afficher la progression
self.label = QLabel('0%')
layout.addWidget(self.label)
# Ajout d'un bouton pour démarrer le thread
self.button = QPushButton('Démarrer')
self.button.clicked.connect(self.start_thread)
layout.addWidget(self.button)
# Configuration du layout pour la fenêtre
self.setLayout(layout)
def start_thread(self):
# Désactiver le bouton pour éviter le démarrage multiple du thread
self.button.setDisabled(True)
# Initialisation du worker et du thread
self.worker = Worker()
self.thread = QThread()
# Configuration du worker pour s'exécuter dans le thread
self.worker.moveToThread(self.thread)
self.worker.progress.connect(self.update_progress)
self.worker.finished.connect(self.on_thread_finished)
# Configuration du thread pour exécuter le worker
self.thread.started.connect(self.worker.run)
# Démarrer le thread
self.thread.start()
def update_progress(self, value):
self.label.setText(f'{value}%')
def on_thread_finished(self):
self.thread.quit()
self.thread.wait() # Attendre que le thread se termine complètement
# Réactiver le bouton une fois le thread terminé
self.button.setEnabled(True)
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec())
Editor is loading...