Advertisement
Guest User

PYQT5 ScrollArea

a guest
Jun 15th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. from PyQt5.QtWidgets import (QWidget, QSlider, QLineEdit, QLabel, QPushButton, QScrollArea,QApplication,
  2.                              QHBoxLayout, QVBoxLayout, QMainWindow)
  3. from PyQt5 import QtWidgets, uic, QtCore
  4. import sys
  5. import threading
  6.  
  7.  
  8. sys._excepthook = sys.excepthook
  9. def exception_hook(exctype, value, traceback):
  10.     print(exctype, value, traceback)
  11.     sys._excepthook(exctype, value, traceback)
  12.     sys.exit(1)
  13. sys.excepthook = exception_hook
  14.  
  15. #A separate thread where the task is doing its work
  16. class TaskThread(QtCore.QObject):
  17.     def __init__(self, num):
  18.         QtCore.QObject.__init__(self)
  19.         self.num = num
  20.  
  21.     def run(self):
  22.         #Random, heavy, task. Takes very long to complete.
  23.         placeholder = []
  24.  
  25.         for i in range(5000):
  26.             for _ in range(200):
  27.                 placeholder.append(self.num**i//3)
  28.  
  29. """
  30. Task is an object containing various data
  31. That task is then executed in the TaskThread
  32. """
  33. class Task:
  34.     def __init__(self, num):
  35.         self.num = num
  36.        
  37.     def start(self):
  38.         self.task = TaskThread(self.num)
  39.         self.thread = threading.Thread(target=self.task.run)
  40.         self.thread.start()
  41.  
  42. """
  43. A thread handling all the task operations.
  44. Starting all tasks from the MainThread would cause blocking
  45. """
  46. class TaskRunner(QtCore.QObject):
  47.     signal = QtCore.pyqtSignal("PyQt_PyObject")
  48.  
  49.     def __init__(self, parent=None):
  50.         super().__init__(parent)
  51.         global tasks
  52.         tasks = []
  53.  
  54.     def receiver(self, msg):
  55.         threading.currentThread().name = "Task-Runner"
  56.         #Start all the tasks
  57.         if msg == "start":
  58.             print("STARTING TASKS FROM THREAD :", QtCore.QThread.currentThread())
  59.             for task in tasks:
  60.                 task.start()
  61.             print(threading.enumerate())
  62.  
  63.         #Load all the TaskObjetcs into the tasks list
  64.         elif msg == "load":
  65.             print("LOADING TASKS FROM THREAD :", QtCore.QThread.currentThread())
  66.             for i in range(200):
  67.                 task = Task(i)
  68.                 tasks.append(task)
  69.  
  70. class MainWindow(QMainWindow):
  71.     signal = QtCore.pyqtSignal("PyQt_PyObject")
  72.     def __init__(self):
  73.         super().__init__()
  74.         self.initUI()
  75.  
  76.     def initUI(self):
  77.         print("GUI LIVES IN :", QtCore.QThread.currentThread())
  78.         """
  79.        Create a separate thread to start all the tasks from. Starting them
  80.        from the MainThread, where the GUI "Lives", will cause blocking.
  81.        """
  82.         self.task_runner = TaskRunner()
  83.         worker_thread = QtCore.QThread(self)
  84.         worker_thread.setObjectName("Task-Runner")
  85.         self.task_runner.moveToThread(worker_thread)
  86.         self.signal.connect(self.task_runner.receiver)
  87.         worker_thread.start()
  88.  
  89.         #Create Scroll Area
  90.         self.scroll = QScrollArea()
  91.         self.widget = QWidget()
  92.         self.vbox = QVBoxLayout()
  93.  
  94.         #For simplicity, just make the top label a StartButton
  95.         self.start_btn = QLabel("START")
  96.         self.vbox.addWidget(self.start_btn)
  97.         self.start_btn.mousePressEvent = self.start_sig
  98.  
  99.         #Create a bunch of labels for the example
  100.         for _ in range(200):
  101.             obj = QLabel("TextLabel")
  102.             self.vbox.addWidget(obj)
  103.  
  104.         #All labels have been loaded
  105.         self.signal.emit("load")
  106.  
  107.         #Scroll Area Properties
  108.         self.widget.setLayout(self.vbox)
  109.         self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
  110.         self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  111.         self.scroll.setWidgetResizable(True)
  112.         self.scroll.setWidget(self.widget)
  113.  
  114.         self.setCentralWidget(self.scroll)
  115.  
  116.         self.setGeometry(600, 100, 1000, 900)
  117.         self.setWindowTitle('Scroll Area Demonstration')
  118.         self.show()
  119.  
  120.         return
  121.  
  122.     def start_sig(self,event):
  123.         self.signal.emit("start")
  124.  
  125. def main():
  126.     app = QtWidgets.QApplication(sys.argv)
  127.     main = MainWindow()
  128.     sys.exit(app.exec_())
  129.  
  130. if __name__ == '__main__':
  131.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement