From Emerald Bat, 2 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=Worker()
  12.         self.thread=QThread()
  13.         self.worker.moveToThread(self.thread)
  14.         self.worker.job_done.connect(lambda m:self.update_label(m,"printing"))
  15.         self.msg="Initialize"
  16.         self.initUI()
  17.         self.set_events()
  18.  
  19.     def initUI(self):
  20.         self.vbox=QVBoxLayout()
  21.  
  22.         self.label=QLabel()
  23.         self.label.setText(self.msg)
  24.  
  25.         self.entry=QLineEdit()
  26.  
  27.         self.pushb=QPushButton("Print")
  28.  
  29.         self.vbox.addWidget(self.label)
  30.         self.vbox.addWidget(self.entry)
  31.         self.vbox.addWidget(self.pushb)
  32.  
  33.         self.setLayout(self.vbox)
  34.  
  35.     def set_events(self):
  36.         self.pushb.clicked.connect(self.set_action)
  37.  
  38.     def set_action(self):
  39.         self.worker.msg=self.entry.text()
  40.         self.thread.start()
  41.        
  42.     def update_label(self,msg,t):
  43.         print(t)
  44.         self.label.setText(msg)
  45.  
  46. class Worker(QObject):
  47.     job_done=pyqtSignal("QString")
  48.  
  49.     def __init__(self, parent=None):
  50.         super(Worker, self).__init__(parent)
  51.         self.msg=None
  52.  
  53.     def print_msg(self):
  54.         time.sleep(5)
  55.         self.job_done.emit("Entered:"+self.msg)
  56.  
  57.     def run(self):
  58.         self.print_msg()
  59.  
  60.  
  61. app=QApplication(sys.argv)
  62. ex=App()
  63.  
  64. ex.show()
  65.  
  66. sys.exit(app.exec_())
captcha