PyQt5练习-多线程的执行和停止

前言

最近在学习pyqt5相关知识,以前都是用C进行项目开发,没有接触过多线程的概念,下面根据其他博主的文章,自己仿着写了个小demo。
参考博文链接
在这里插入图片描述

代码

这边没用qtdesigner进行画面制作,而是直接使用代码进行控件创建,对于我这个初学者来说,是有点麻烦,接口不是很熟悉,不过谁让我懒呢😎。我就想着下次只需要复制一次代码,右击执行,就可以看到效果😏

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QPushButton, QProgressBar,QWidget,QGridLayout,QLabel

import sys,time

#创建线程计数列表,储存当前线程对应计数
Thread_count = [0,0,0,0]
class THREAD_APP(QWidget):
    def __init__(self):
        super().__init__()
        self.Init_Ui()

        self.thread = {}
        #绑定槽函数
        self.thread_button_star_1.clicked.connect(self.start_thread_1)
        self.thread_button_star_2.clicked.connect(self.start_thread_2)
        self.thread_button_star_3.clicked.connect(self.start_thread_3)
        self.thread_button_stop_1.clicked.connect(self.stop_thread_1)
        self.thread_button_stop_2.clicked.connect(self.stop_thread_2)
        self.thread_button_stop_3.clicked.connect(self.stop_thread_3)
        self.thread_button_stop_1.setEnabled(False)
        self.thread_button_stop_2.setEnabled(False)
        self.thread_button_stop_3.setEnabled(False)


    def Init_Ui(self):

        #设置窗体位置和大小(int x,int y,int width,int height)
        self.setGeometry(400, 400, 500, 50)

        self.thread_progressbar_1 = QProgressBar()
        self.thread_progressbar_lab_1 = QLabel()
        self.thread_button_star_1 = QPushButton("start thread 1")
        self.thread_button_stop_1 = QPushButton("stop thread 1")


        self.thread_progressbar_2 = QProgressBar()
        self.thread_progressbar_lab_2 = QLabel()
        self.thread_button_star_2 = QPushButton("start thread 2")
        self.thread_button_stop_2 = QPushButton("stop thread 2")


        self.thread_progressbar_3 = QProgressBar()
        self.thread_progressbar_lab_3 = QLabel()
        self.thread_button_star_3 = QPushButton("start thread 3")
        self.thread_button_stop_3 = QPushButton("stop thread 3")


        layout = QGridLayout(self)
        layout.addWidget(self.thread_button_star_1, 0, 0)
        layout.addWidget(self.thread_button_star_2, 1, 0)
        layout.addWidget(self.thread_button_star_3, 2, 0)

        layout.addWidget(self.thread_button_stop_1, 0, 1)
        layout.addWidget(self.thread_button_stop_2, 1, 1)
        layout.addWidget(self.thread_button_stop_3, 2, 1)

        layout.addWidget(self.thread_progressbar_1, 0, 2)
        layout.addWidget(self.thread_progressbar_2, 1, 2)
        layout.addWidget(self.thread_progressbar_3, 2, 2)

        layout.addWidget(self.thread_progressbar_lab_1, 0, 3)
        layout.addWidget(self.thread_progressbar_lab_1, 1, 3)
        layout.addWidget(self.thread_progressbar_lab_1, 2, 3)



    def start_thread_1(self):
        self.thread[1] = ThreadClass(parent=None,index=1)
        self.thread[1].start()
        self.thread[1].any_signal.connect(self.my_function)
        self.thread_button_star_1.setEnabled(False)
        self.thread_button_stop_1.setEnabled(True)


    def start_thread_2(self):
        self.thread[2] = ThreadClass(parent=None, index=2)
        self.thread[2].start()
        self.thread[2].any_signal.connect(self.my_function)
        self.thread_button_star_2.setEnabled(False)
        self.thread_button_stop_2.setEnabled(True)

    def start_thread_3(self):
        self.thread[3] = ThreadClass(parent=None,index=3)
        self.thread[3].start()
        self.thread[3].any_signal.connect(self.my_function)
        self.thread_button_star_3.setEnabled(False)
        self.thread_button_stop_3.setEnabled(True)

    def stop_thread_1(self):
        self.thread[1].stop()
        self.thread_button_star_1.setEnabled(True)
        self.thread_button_stop_1.setEnabled(False)

    def stop_thread_2(self):
        self.thread[2].stop()
        self.thread_button_star_2.setEnabled(True)
        self.thread_button_stop_2.setEnabled(False)

    def stop_thread_3(self):
        self.thread[3].stop()
        self.thread_button_star_3.setEnabled(True)
        self.thread_button_stop_3.setEnabled(False)

    def my_function(self,counter):
        #获取信号发送对象
        index = self.sender().index
        if index == 1:
            self.thread_progressbar_1.setValue(counter)
        if index == 2:
            self.thread_progressbar_2.setValue(counter)
        if index == 3:
            self.thread_progressbar_3.setValue(counter)

class ThreadClass(QtCore.QThread):
    #创建自定义信号
    any_signal = QtCore.pyqtSignal(int)
    def __init__(self,parent=None,index=0):
        super().__init__()
        self.index = index

    def run(self):
        print('Starting thread...',self.index)
        while(True):
            if Thread_count[self.index]==99: Thread_count[self.index]=0
            #对应线程计数值+1
            Thread_count[self.index]+=1
            time.sleep(0.1)
            self.any_signal.emit(Thread_count[self.index])

    def stop(self):
        print('stopping thread...',self.index)
        self.terminate()

if __name__ == '__main__':
    app=QtWidgets.QApplication(sys.argv)
    mainWindow = THREAD_APP()
    mainWindow.show()
    sys.exit(app.exec_())

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值