Thread 多线程

from PyQt5.Qt import *

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.setup_ui()
        self.resize(500,500)
        self.thread={}

    def setup_ui(self):
        btn1=QPushButton("START1")
        btn2=QPushButton("START2")
        btn3=QPushButton("START3")
        btn4=QPushButton("STOP4")
        btn5=QPushButton("STOP5")
        btn6=QPushButton("STOP6")
        self.btn1 = btn1
        self.btn2 = btn2
        self.btn3 = btn3
        self.btn4 = btn4
        self.btn5 = btn5
        self.btn6 = btn6


        pro_bar1=QProgressBar()
        pro_bar2=QProgressBar()
        pro_bar3=QProgressBar()
        self.pro_bar1=pro_bar1
        self.pro_bar2=pro_bar2
        self.pro_bar3=pro_bar3



        h1_layout=QHBoxLayout()
        h2_layout=QHBoxLayout()
        h3_layout=QHBoxLayout()
        h1_layout.addWidget(btn1)
        h1_layout.addWidget(btn4)
        h1_layout.addWidget(pro_bar1)

        h2_layout.addWidget(btn2)
        h2_layout.addWidget(btn5)
        h2_layout.addWidget(pro_bar2)

        h3_layout.addWidget(btn3)
        h3_layout.addWidget(btn6)
        h3_layout.addWidget(pro_bar3)

        layout=QVBoxLayout()
        layout.addLayout(h1_layout)
        layout.addLayout(h2_layout)
        layout.addLayout(h3_layout)


        group=QGroupBox("Thread",self)
        group.setLayout(layout)
        group.resize(800,300)

        self.setStyleSheet("QPushButton{background-color:cyan},QProgressBar{background-color:green}")
    def single_connect(self):
        self.btn1.clicked.connect(self.start_work_1)
        self.btn2.clicked.connect(self.start_work_2)
        self.btn3.clicked.connect(self.start_work_3)
        self.btn4.clicked.connect(self.stop_work_1)
        self.btn5.clicked.connect(self.stop_work_2)
        self.btn6.clicked.connect(self.stop_work_3)
    def start_work_1(self):
        self.thread[1] = ThreadClass(parent=None,index=1)
        self.thread[1].start()
        self.thread[1].any_singal.connect(self.my_function)
        self.btn1.setEnabled(False)
    def start_work_2(self):
        self.thread[2] = ThreadClass(parent=None,index=2)
        self.thread[2].start()
        self.thread[2].any_singal.connect(self.my_function)
        self.btn1.setEnabled(False)
    def start_work_3(self):
        self.thread[3] = ThreadClass(parent=None, index=3)
        self.thread[3].start()
        self.thread[3].any_singal.connect(self.my_function)
        self.btn1.setEnabled(False)
    def stop_work_1(self):
        self.thread[1].stop()
        self.btn1.setEnabled(True)

    def stop_work_2(self):
        self.thread[2].stop()
        self.btn1.setEnabled(True)
    def stop_work_3(self):
        self.thread[3].stop()
        self.btn1.setEnabled(True)
    def myfunction(self,counter):
        cnt=counter
        index=self.sender().index
        if index==1:
            self.pro_bar1.setValue(cnt)
        if index == 2:
            self.pro_bar2.setValue(cnt)
        if index == 3:
            self.pro_bar3.setValue(cnt)


class ThreadClass(QThread):
    any_singal=pyqtSignal(int)
    def __int__(self,parent=None,index=0):
        super().__init__(parent)
        self.index=index
        self.is_running=True
    def run(self):
        import time
        print("starting thread...",self.index)
        cnt=0
        while True:
            cnt+=1
            if cnt==99:cnt=0
            time.sleep(0.01)
            self.any_singal.emit(cnt)
    def stop(self):
        self.is_running=False
        print('stoppint thread...',self.index)
        self.terminate()




if __name__ == '__main__':
    import sys
    app=QApplication(sys.argv)
    win=Window()
    win.show()
    sys.exit(app.exec_())

上次这个是看国外视频课程照写的,当初也没仔细研究,今天重新研究了以下self.sender()这个到底怎么用的。这个方法很有用,之前在按钮上用过,当时理解不是特别深入,sender()写在槽函数里面,当有好几个控件绑定在同一个槽函数,我们需要知道是哪个控件触发的,这是用这个就很方便了。sender就是该控件对象。我们就可以查看它的属性,上面这个例子我用了index=self.sender().index ,为什么呢?这里的sender是一个Thready对象。用print(self.sender().__dict__)结果如下:

{'index': 1, 'is_running': True}
{'index': 2, 'is_running': True}
{'index': 3, 'is_running': True}

下面是今天写的代码


from PyQt5.Qt import *
from thread_ui import Ui_Form
import time



class Window(QWidget,Ui_Form):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("pa的学习")
        self.resize(500,500)
        self.setupUi(self)

        self.thread={}
        self.setup_ui()
    def setup_ui(self):
        self.pushButton.clicked.connect(self.start_work_1)
        self.pushButton_3.clicked.connect(self.start_work_2)
        self.pushButton_5.clicked.connect(self.start_work_3)

        self.pushButton_2.clicked.connect(self.stop_work_1)
        self.pushButton_4.clicked.connect(self.stop_work_2)
        self.pushButton_6.clicked.connect(self.stop_work_3)

    def start_work_1(self):
        self.thread[1]=MyThread(1,self)
        self.thread[1].singal_value.connect(self.my_fun)
        self.thread[1].start()
        self.pushButton.setEnabled(False)
    def start_work_2(self):
        self.thread[2]=MyThread(2,self)
        self.thread[2].singal_value.connect(self.my_fun)
        self.thread[2].start()
        self.pushButton_3.setEnabled(False)
    def start_work_3(self):
        self.thread[3]=MyThread(3,self)
        self.thread[3].singal_value.connect(self.my_fun)
        self.thread[3].start()
        self.pushButton_5.setEnabled(False)


    def stop_work_1(self):
        print('1')
    def stop_work_2(self):
        print("2")
    def stop_work_3(self):
        print("3")
    def my_fun(self,count):
        index=self.sender().index
        print(self.sender().__dict__)
        if index==1:
            self.progressBar.setValue(count)
        if index==2:
            self.progressBar_2.setValue(count)
        if index==3:
            self.progressBar_3.setValue(count)


class MyThread(QThread):
    singal_value=pyqtSignal(int)
    def __init__(self,index,parent=None,*args,**kwargs):
        super(MyThread, self).__init__(parent)
        self.index=index
        self.is_running=True
    def run(self):
        print(f"{self.index}#threading is running")
        cnt=0
        while True:
            cnt+=1
            if cnt==99:break
            self.singal_value.emit(cnt)
            time.sleep(1)
    def stop(self):
        self.is_running=False
        print("stopping thread")
        self.terminate()




if __name__ == '__main__':
    import sys
    app=QApplication(sys.argv)
    win=Window()
    win.show()
    sys.exit(app.exec_())


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(715, 320)
        self.frame = QtWidgets.QFrame(Form)
        self.frame.setGeometry(QtCore.QRect(70, 40, 551, 161))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.gridLayout = QtWidgets.QGridLayout(self.frame)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton_4 = QtWidgets.QPushButton(self.frame)
        self.pushButton_4.setStyleSheet("background-color: rgb(255, 0, 0);")
        self.pushButton_4.setObjectName("pushButton_4")
        self.gridLayout.addWidget(self.pushButton_4, 2, 2, 1, 1)
        self.pushButton_3 = QtWidgets.QPushButton(self.frame)
        self.pushButton_3.setStyleSheet("background-color: rgb(28, 255, 47);")
        self.pushButton_3.setObjectName("pushButton_3")
        self.gridLayout.addWidget(self.pushButton_3, 2, 0, 1, 1)
        self.progressBar_3 = QtWidgets.QProgressBar(self.frame)
        self.progressBar_3.setProperty("value", 24)
        self.progressBar_3.setObjectName("progressBar_3")
        self.gridLayout.addWidget(self.progressBar_3, 4, 3, 1, 1)
        self.pushButton_2 = QtWidgets.QPushButton(self.frame)
        self.pushButton_2.setStyleSheet("background-color: rgb(255, 0, 0);")
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 0, 2, 1, 1)
        self.progressBar = QtWidgets.QProgressBar(self.frame)
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        self.gridLayout.addWidget(self.progressBar, 0, 3, 1, 1)
        self.pushButton_6 = QtWidgets.QPushButton(self.frame)
        self.pushButton_6.setStyleSheet("background-color: rgb(255, 0, 0);")
        self.pushButton_6.setObjectName("pushButton_6")
        self.gridLayout.addWidget(self.pushButton_6, 4, 2, 1, 1)
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setStyleSheet("background-color: rgb(28, 255, 47);")
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        self.pushButton_5 = QtWidgets.QPushButton(self.frame)
        self.pushButton_5.setStyleSheet("background-color: rgb(28, 255, 47);")
        self.pushButton_5.setObjectName("pushButton_5")
        self.gridLayout.addWidget(self.pushButton_5, 4, 0, 1, 1)
        self.progressBar_2 = QtWidgets.QProgressBar(self.frame)
        self.progressBar_2.setProperty("value", 24)
        self.progressBar_2.setObjectName("progressBar_2")
        self.gridLayout.addWidget(self.progressBar_2, 2, 3, 1, 1)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton_4.setText(_translate("Form", "stop_thread2"))
        self.pushButton_3.setText(_translate("Form", "start_thread2"))
        self.pushButton_2.setText(_translate("Form", "stop_thread1"))
        self.pushButton_6.setText(_translate("Form", "stop_thread3"))
        self.pushButton.setText(_translate("Form", "start_thread1"))
        self.pushButton_5.setText(_translate("Form", "start_thread3"))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值