PyQt5多线程使用

26 篇文章 0 订阅

PyQt5多线程使用

本案例使用PyQt5多线程实现一个UI界面同时显示3个时间实时更新控件,从而直观地了解到Qt多线程是如何进行工作的。

from PyQt5.QtCore import QThread,pyqtSignal,QDateTime
from PyQt5.QtWidgets import QApplication,QDialog,QLineEdit,QVBoxLayout
import time
import sys

class Thread1(QThread): #创建线程1的具体实现
    update_date=pyqtSignal(str)  #设置信号槽,发送符串参数
    def run(self):
        while True:
            data=QDateTime.currentDateTime()  #读取系统时间
            currentTime=data.toString("yyyy-MM-dd  hh:mm:ss")  #更改显示格式
            self.update_date.emit(str(currentTime))  #触发信号槽,发送一个字符串信号
            time.sleep(1) #设置休眠时间

class Thread2(QThread): #创建线程2的具体实现
    update_date=pyqtSignal(str)  #设置信号槽,发送字符串参数
    def run(self):
        while True:
            data=QDateTime.currentDateTime()  #读取系统时间
            currentTime=data.toString("yyyy-MM-dd  hh:mm:ss")  #更改时间显示格式
            self.update_date.emit(str(currentTime))  #触发信号槽,发送一个字符串信号
            time.sleep(2) #设置休眠时间

class Thread3(QThread): #创建线程3的具体实现
    update_date=pyqtSignal(str)  #设置信号槽,发送字符串参数
    def run(self):
        while True:
            data=QDateTime.currentDateTime()  #读取系统时间
            currentTime=data.toString("yyyy-MM-dd  hh:mm:ss")  #更改时间显示格式
            self.update_date.emit(str(currentTime))  #触发信号槽,发送一个字符串信号
            time.sleep(3) #设置休眠时间

class ThreadDialog(QDialog):#主窗口类
    def  __init__(self):
        QDialog.__init__(self)  #初始化
        self.setWindowTitle('Qt多线程') #设置窗口标题
        self.resize(400,100)  #设置窗口大小
        self.initUI()
        
    def initUI(self):
        self.lineEdit1 = QLineEdit(self)  # 创建QLineEdit1控件
        self.lineEdit2 = QLineEdit(self)  # 创建QLineEdit2控件
        self.lineEdit3 = QLineEdit(self)  # 创建QLineEdit3控件
        self.vbox = QVBoxLayout() # 创建垂直布局
        self.vbox.addWidget(self.lineEdit1) # 添加控件lineEdit1
        self.vbox.addWidget(self.lineEdit2) # 添加控件lineEdit2
        self.vbox.addWidget(self.lineEdit3) # 添加控件lineEdit3
        self.setLayout(self.vbox) # 设置本窗口布局为垂直布局
        self.thread1 = Thread1()  # 创建线程1
        self.thread2 = Thread2()  # 创建线程2
        self.thread3 = Thread3()  # 创建线程3
        self.thread1.update_date.connect(self.lineEditDisplay1)  # 线程1更新时间
        self.thread2.update_date.connect(self.lineEditDisplay2)  # 线程2更新时间
        self.thread3.update_date.connect(self.lineEditDisplay3)  # 线程3更新时间
        self.thread1.start()  # 启动线程1
        self.thread2.start()  # 启动线程2
        self.thread3.start()  # 启动线程3

    def lineEditDisplay1(self,data): #信号槽函数,接收字符串参数
        self.lineEdit1.setText(data)

    def lineEditDisplay2(self,data): #信号槽函数,接收字符串参数
        self.lineEdit2.setText(data)

    def lineEditDisplay3(self,data): #信号槽函数,接收字符串参数
        self.lineEdit3.setText(data)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    th = ThreadDialog()
    th.show()
    sys.exit(app.exec_())

QThread

PyQt5中的多线程定时器可以通过使用`QTimer`类和Python的`threading`模块来实现。由于直接在GUI线程中执行长时间运行的任务可能会导致界面冻结,因此使用多线程可以避免这种情况。下面是如何在PyQt5使用多线程定时器的基本方法: 1. 首先,创建一个`QTimer`对象,并设置其超时时间。 2. 然后,创建一个工作线程,用于在定时器超时时执行任务。 3. 使用信号与槽机制,将定时器的超时信号连接到工作线程中的槽函数,这样定时器触发时可以通知工作线程执行任务。 4. 最后,启动定时器和工作线程。 下面是一个简单的示例代码,展示了如何使用PyQt5中的多线程定时器: ```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton from PyQt5.QtCore import QTimer, QThread, pyqtSignal import time class Worker(QThread): change_text = pyqtSignal(str) def run(self): """线程的任务,这里是一个简单的无限循环,每隔一定时间改变窗口中的文本""" for i in range(10): time.sleep(1) # 模拟耗时操作 self.change_text.emit(str(i)) # 发射信号 class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.button = QPushButton('启动定时器', self) self.button.clicked.connect(self.startTimer) self.label = QLabel('定时器未启动') self.setCentralWidget(self.label) self.timer = QTimer(self) self.timer.setInterval(2000) # 定时器每2秒触发一次 self.timer.timeout.connect(self.updateLabel) # 定时器超时调用updateLabel def startTimer(self): if not self.timer.isActive(): self.worker = Worker() self.worker.change_text.connect(self.updateLabel) self.worker.start() def updateLabel(self, text=''): self.label.setText(text) if __name__ == '__main__': app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit(app.exec_()) ``` 在这个示例中,我们创建了一个`Worker`类,它继承自`QThread`并重写了`run`方法。`run`方法中包含一个无限循环,每隔一秒钟发射一个信号,该信号携带要更新的文本。`MainWindow`类中有一个定时器,每隔两秒触发一次,并调用`updateLabel`方法来更新标签的文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值