QMainWindow多线程demo
【摘要】
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets, QtCore
import sys
from PyQt5.QtCore import *
import time
# 继承QThread
from PyQt5.QtWidgets import QMainWindow, QPushButton
class ...
# -*- coding: utf-8 -*- from PyQt5 import QtWidgets, QtCore import sys from PyQt5.QtCore import * import time # 继承QThread from PyQt5.QtWidgets import QMainWindow, QPushButton class Runthread(QThread): # python3,pyqt5与之前的版本有些不一样 # 通过类成员对象定义信号对象 _signal = pyqtSignal(str) def __init__(self, parent=None): super(Runthread, self).__init__() def __del__(self): self.wait() def run(self): # 处理你要做的业务逻辑,这里是通过一个回调来处理数据,这里的逻辑处理写自己的方法 # wechat.start_auto(self.callback) for i in range(2000000): # 发出信号 self._signal.emit('当前循环值为:%s' % i) # 让程序休眠 time.sleep(0.5) # self._signal.emit("sdf"); #可以在这里写信号焕发 def callback(self, msg): # 信号焕发,我是通过我封装类的回调来发起的 self._signal.emit(msg); class mywindow(QMainWindow): def __init__(self): super(mywindow, self).__init__() self.setupUi() def setupUi(self): self.btn1 = QPushButton("Button 1", self) self.btn1.setMaximumWidth(200) self.btn1.move(30, 50) btn2 = QPushButton("Button 2", self) btn2.move(150, 50) self.btn1.clicked.connect(self.start_login) # self.btn1.clicked.connect(self.buttonClicked) btn2.clicked.connect(self.start_login) self.statusBar() self.setGeometry(300, 300, 290, 150) self.setWindowTitle('Event sender') self.show() def start_login(self): # 创建线程 self.thread = Runthread() # 连接信号 self.thread._signal.connect(self.callbacklog) # 开始线程 self.thread.start() def callbacklog(self, msg): # 奖回调数据输出到文本框 self.btn1.setText("\n"+ msg+ " "+ time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())); if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) myshow = mywindow() myshow.show() sys.exit(app.exec_())
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/78785217
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)