Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtWidgets import (QWidget, QSlider, QLineEdit, QLabel, QPushButton, QScrollArea,QApplication,
- QHBoxLayout, QVBoxLayout, QMainWindow)
- from PyQt5 import QtWidgets, uic, QtCore
- import sys
- import threading
- sys._excepthook = sys.excepthook
- def exception_hook(exctype, value, traceback):
- print(exctype, value, traceback)
- sys._excepthook(exctype, value, traceback)
- sys.exit(1)
- sys.excepthook = exception_hook
- #A separate thread where the task is doing its work
- class TaskThread(QtCore.QObject):
- def __init__(self, num):
- QtCore.QObject.__init__(self)
- self.num = num
- def run(self):
- #Random, heavy, task. Takes very long to complete.
- placeholder = []
- for i in range(5000):
- for _ in range(200):
- placeholder.append(self.num**i//3)
- """
- Task is an object containing various data
- That task is then executed in the TaskThread
- """
- class Task:
- def __init__(self, num):
- self.num = num
- def start(self):
- self.task = TaskThread(self.num)
- self.thread = threading.Thread(target=self.task.run)
- self.thread.start()
- """
- A thread handling all the task operations.
- Starting all tasks from the MainThread would cause blocking
- """
- class TaskRunner(QtCore.QObject):
- signal = QtCore.pyqtSignal("PyQt_PyObject")
- def __init__(self, parent=None):
- super().__init__(parent)
- global tasks
- tasks = []
- def receiver(self, msg):
- threading.currentThread().name = "Task-Runner"
- #Start all the tasks
- if msg == "start":
- print("STARTING TASKS FROM THREAD :", QtCore.QThread.currentThread())
- for task in tasks:
- task.start()
- print(threading.enumerate())
- #Load all the TaskObjetcs into the tasks list
- elif msg == "load":
- print("LOADING TASKS FROM THREAD :", QtCore.QThread.currentThread())
- for i in range(200):
- task = Task(i)
- tasks.append(task)
- class MainWindow(QMainWindow):
- signal = QtCore.pyqtSignal("PyQt_PyObject")
- def __init__(self):
- super().__init__()
- self.initUI()
- def initUI(self):
- print("GUI LIVES IN :", QtCore.QThread.currentThread())
- """
- Create a separate thread to start all the tasks from. Starting them
- from the MainThread, where the GUI "Lives", will cause blocking.
- """
- self.task_runner = TaskRunner()
- worker_thread = QtCore.QThread(self)
- worker_thread.setObjectName("Task-Runner")
- self.task_runner.moveToThread(worker_thread)
- self.signal.connect(self.task_runner.receiver)
- worker_thread.start()
- #Create Scroll Area
- self.scroll = QScrollArea()
- self.widget = QWidget()
- self.vbox = QVBoxLayout()
- #For simplicity, just make the top label a StartButton
- self.start_btn = QLabel("START")
- self.vbox.addWidget(self.start_btn)
- self.start_btn.mousePressEvent = self.start_sig
- #Create a bunch of labels for the example
- for _ in range(200):
- obj = QLabel("TextLabel")
- self.vbox.addWidget(obj)
- #All labels have been loaded
- self.signal.emit("load")
- #Scroll Area Properties
- self.widget.setLayout(self.vbox)
- self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
- self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
- self.scroll.setWidgetResizable(True)
- self.scroll.setWidget(self.widget)
- self.setCentralWidget(self.scroll)
- self.setGeometry(600, 100, 1000, 900)
- self.setWindowTitle('Scroll Area Demonstration')
- self.show()
- return
- def start_sig(self,event):
- self.signal.emit("start")
- def main():
- app = QtWidgets.QApplication(sys.argv)
- main = MainWindow()
- sys.exit(app.exec_())
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement