43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import sys
|
|
from PyQt5.QtCore import QThread, pyqtSignal
|
|
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget
|
|
|
|
# Worker Thread
|
|
class Worker(QThread):
|
|
progress = pyqtSignal(int) # Signal to send data to the main thread
|
|
|
|
def run(self):
|
|
for i in range(100):
|
|
self.sleep(1) # simulate long task
|
|
self.progress.emit(i) # emit progress update
|
|
|
|
# Main Window
|
|
class MainWindow(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setWindowTitle("QThread Example")
|
|
self.setGeometry(100, 100, 300, 150)
|
|
|
|
self.label = QLabel("Press Start", self)
|
|
self.button = QPushButton("Start Long Task", self)
|
|
self.button.clicked.connect(self.start_task)
|
|
|
|
layout = QVBoxLayout()
|
|
layout.addWidget(self.label)
|
|
layout.addWidget(self.button)
|
|
self.setLayout(layout)
|
|
|
|
def start_task(self):
|
|
self.worker = Worker()
|
|
self.worker.progress.connect(self.update_label)
|
|
self.worker.start()
|
|
|
|
def update_label(self, value):
|
|
self.label.setText(f"Count: {value}")
|
|
|
|
# Run the app
|
|
app = QApplication(sys.argv)
|
|
window = MainWindow()
|
|
window.show()
|
|
sys.exit(app.exec_())
|