From Aqua Coyote, 3 Weeks ago, written in Plain Text.
  1. import sys
  2. import time
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtWidgets import *
  5. from PyQt5.QtGui import *
  6.  
  7. class App(QWidget):
  8.     def __init__(self):
  9.         super().__init__()
  10.         self.setWindowTitle("QThread Example")
  11.         self.worker=WorkerThread()
  12.         self.worker.job_done.connect(lambda m:self.update_label(m,"printing"))
  13.         self.msg="Initialize"
  14.         self.initUI()
  15.         self.set_events()
  16.  
  17.     def initUI(self):
  18.         self.vbox=QVBoxLayout()
  19.  
  20.         self.label=QLabel()
  21.         self.label.setText(self.msg)
  22.  
  23.         self.entry=QLineEdit()
  24.  
  25.         self.pushb=QPushButton("Print")
  26.  
  27.         self.vbox.addWidget(self.label)
  28.         self.vbox.addWidget(self.entry)
  29.         self.vbox.addWidget(self.pushb)
  30.  
  31.         self.setLayout(self.vbox)
  32.  
  33.     def set_events(self):
  34.         self.pushb.clicked.connect(self.set_action)
  35.  
  36.     def set_action(self):
  37.         self.worker.msg=self.entry.text()
  38.         self.worker.start()
  39.  
  40.     def update_label(self,msg,t):
  41.         print(t)
  42.         self.label.setText(msg)
  43.  
  44. class WorkerThread(QThread):
  45.     job_done=pyqtSignal("QString")
  46.  
  47.     def __init__(self, parent=None):
  48.         super(WorkerThread, self).__init__(parent)
  49.         self.msg=None
  50.  
  51.     def print_msg(self):
  52.         time.sleep(5)
  53.         self.job_done.emit("Entered:"+self.msg)
  54.  
  55.     def run(self):
  56.         self.print_msg()
  57.  
  58.  
  59. app=QApplication(sys.argv)
  60. ex=App()
  61.  
  62. ex.show()
  63.  
  64. sys.exit(app.exec_())
  65.  
captcha