对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-numpy实现灰度图像的分块和合并方式

我就废话不多说了,直接上代码吧! from numpy import * import numpy as np import cv2, os, math, os.path from...

Python中实现单例模式的n种方式和原理

在Python中如何实现单例模式?这可以说是一个经典的Python面试题了。这回我们讲讲实现Python中实现单例模式的n种方式,和它的原理。 什么是单例模式 维基百科 中说: 单例模式...

Python查找文件中包含中文的行方法

前言 近几天在做多语言版本的时候再次发现,区分各种语言真的是一件比较困难的事情,上一次做中文提取工具的就花了不少时间,这次决定用python试一试,结果写起来发现真是方便不少,自己整理了...

python常见排序算法基础教程

python常见排序算法基础教程

前言:前两天腾讯笔试受到1万点暴击,感觉浪费我两天时间去牛客网做题……这篇博客介绍几种简单/常见的排序算法,算是整理下。 时间复杂度 (1)时间频度一个算法执行所耗费的时间,从理论上是不...

Python脚本在Appium库上对移动应用实现自动化测试

 采用Appium进行自动化的功能性测试最酷的一点是,你可以使用具有最适合你的测试工具的任何一门语言来写你的测试代码。大家选择最多的一个测试编程语言就是Python。 使用Ap...