对pyqt5多线程正确的开启姿势详解

yipeiwu_com6年前Python基础

如下所示:

# -*- coding: utf-8 -*-
 
import sys
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QMessageBox, \
  QPushButton, QLineEdit, QLabel, QToolTip, QComboBox, QTextEdit
 
 
class MyBeautifulClass(QMainWindow):
  def __init__(self):
    super(MyBeautifulClass, self).__init__()
    self.init_ui()
 
  def init_ui(self):
    self.resize(1000, 800)
    self.setWindowTitle('Demo of PyQt5 QThread')
    self.btn_1 = QPushButton('start', self)
    self.btn_1.setGeometry(100, 100, 100, 50)
    self.btn_1.clicked.connect(self.slot_btn_1)
    self.linEdit_2 = QLineEdit(self)
    self.linEdit_2.setGeometry(100, 400, 300, 50)
 
  def slot_btn_1(self):
    self.mbt = MyBeautifulThread()
    self.mbt.trigger.connect(self.slot_thread)
    self.mbt.start()
 
  def say_love(self):
    print('say love')
 
  def slot_thread(self, msg_1, msg_2):
    self.linEdit_2.setText(msg_1 + msg_2)
 
 
class MyBeautifulThread(QThread):
  trigger = pyqtSignal(str, str)
 
  def __init__(self):
    super(MyBeautifulThread, self).__init__()
 
  def run(self):
    w = MyBeautifulClass()
    w.say_love()
    self.trigger.emit('Lo', 've')
 
 
def main():
  app = QApplication(sys.argv)
  w = MyBeautifulClass()
  w.show()
  sys.exit(app.exec_())
 
 
if __name__ == '__main__':
  main()

以上这篇对pyqt5多线程正确的开启姿势详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python异常处理总结

本文较为详细的罗列了Python常见的异常处理,供大家参考,具体如下: 1. 抛出异常和自定义异常 Python用异常对象(exception object)表示异常情况,遇到错误后,会...

apache部署python程序出现503错误的解决方法

前言 本文主要给大家介绍了解决apahce部署python程序出现503错误的相关内容,下面话不多说了,下一起看看详细的介绍吧。 发现问题 今天更新服务器后,发现使用apache部署的某...

Python中unittest用法实例

本文实例讲述了Python中unittest的用法,分享给大家供大家参考。具体用法分析如下: 1. unittest module包含了编写运行unittest的功能,自定义的test...

基于python的多进程共享变量正确打开方式

多进程共享变量和获得结果 由于工程需求,要使用多线程来跑一个程序。但是因为听说python的多线程是假的,于是使用多进程,反正任务需要共享的参数少。 查阅资料,发现实现多进程主要使用Mu...

python opencv 实现对图像边缘扩充

python opencv 实现对图像边缘扩充

原始图像 根据图像的边界的像素值,向外扩充图片,每个方向扩充50个像素。 a = cv2.copyMakeBorder(img,50,50,50,50,cv2.BORDER_REPL...