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

yipeiwu_com5年前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设计】。

相关文章

Python3.6中Twisted模块安装的问题与解决

Python3.6中Twisted模块安装的问题与解决

发现问题 今天准备学习爬虫的scrapy模块,在这之前需要安装许多别的模块,Twisted就是其一 一开始想着直接用pycharm来安装就行了,没想到安装了一会就报错了,如下 后来就换...

python 有效的括号的实现代码示例

给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭...

.dcm格式文件软件读取及python处理详解

要处理一些.DCM格式的焊接缺陷图像,需要读取和显示.dcm格式的图像。通过搜集资料收集到一些医学影像,并通过pydicom模块查看.dcm格式文件。 若要查看dcm格式文件,可下Ech...

学习python中matplotlib绘图设置坐标轴刻度、文本

学习python中matplotlib绘图设置坐标轴刻度、文本

总结matplotlib绘图如何设置坐标轴刻度大小和刻度。 上代码: from pylab import * from matplotlib.ticker import Multi...

Python中将字典转换为列表的方法

Python中将字典转换为列表的方法

说明:列表不可以转换为字典 ①转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a...