pyqt多线程进度条

时间:2021-08-17 23:01:26

ui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(170, 190, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.progressBar = QtWidgets.QProgressBar(Form)
        self.progressBar.setGeometry(QtCore.QRect(30, 50, 361, 71))
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "PushButton"))

 thread.py

import sys
import time

from PyQt5.QtCore import QThread, pyqtSignal, QTimer
from PyQt5.QtWidgets import QApplication, QWidget

from ui import *

a=[1,2]
class MyWindow(QWidget,Ui_Form):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setupUi(self)

class thread(QThread):
    trigger = pyqtSignal()#这个是传信号的
    i=0
    def run(self):
        for i in range(100):
            self.i=i
            time.sleep(1)
            a[0]=i
            self.trigger.emit()#这里发出一个信号


def sta():
    timer.start(1000)
    th.start()
    th.trigger.connect(shuaxin)#这里监听信号

def shuaxin():
    myWin.progressBar.setProperty("value", a[0]+1) #这里执行发出信号后的内容

if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MyWindow()
    timer = QTimer()
    th = thread()
    myWin.pushButton.clicked.connect(sta)
    myWin.show()
    sys.exit(app.exec_())