Advertisement
DevTricks

QThread

Jul 24th, 2023
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. import sys, time
  2. from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QProgressBar, QTextEdit, QVBoxLayout
  3. from PyQt5.QtCore import Qt
  4.  
  5. from PyQt5.QtCore import QThread
  6.  
  7. class ProgressBarThread(QThread):
  8.     def __init__(self, mainwindow, parent=None):
  9.         super().__init__()
  10.         self.mainwindow = mainwindow
  11.  
  12.     def run(self):
  13.         value = self.mainwindow.progressbar.value()
  14.         while value < 100:
  15.             value = value + 1
  16.             self.mainwindow.progressbar.setValue(value)
  17.             time.sleep(0.2)
  18.  
  19. class MyProgressbarWindow(QDialog):
  20.     def __init__(self, parent=None):
  21.         super().__init__()
  22.         self.progressbar = QProgressBar()
  23.         self.progressbar.setAlignment(Qt.AlignCenter)
  24.         self.PushButtonLaunchLoading = QPushButton('Launch loading')
  25.         self.TextEditer = QTextEdit()
  26.         self.setGeometry(300, 400, 300,150)
  27.  
  28.         vbox = QVBoxLayout()
  29.         vbox.addWidget(self.PushButtonLaunchLoading)
  30.         vbox.addWidget(self.TextEditer)
  31.         vbox.addWidget(self.progressbar)
  32.         self.setLayout(vbox)
  33.  
  34.         self.PushButtonLaunchLoading.clicked.connect(self.launch_progress_bar_filling)
  35.         self.progressbarThread_instance = ProgressBarThread(mainwindow=self)
  36.  
  37.     def launch_progress_bar_filling(self):
  38.         self.progressbarThread_instance.start()
  39.  
  40. app = QApplication(sys.argv)
  41. main = MyProgressbarWindow()
  42. main.show()
  43. sys.exit(app.exec())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement