透明的城市 2022-07-25 23:51 采纳率: 50%
浏览 72
已结题

关于#pyqt5#的线程问题,如何解决?

现在有一个纯数字的列表,我想让它们各自进行倒计时并且输出在UI界面上

t_list = [26,33,10,18,45,6]

我是这样写的

from PyQt5.QtCore import QThread,pyqtSignal
from PyQt5 import QtCore,QtWidgets
import time

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(553, 588)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(470, 210, 75, 23))
        self.pushButton.setObjectName("pushButton")

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 553, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.List = QtWidgets.QTextBrowser(self.centralwidget)
        self.List.setGeometry(20,20,200,300)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.pushButton.clicked.connect(lambda :self.tist())
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "开始"))

    def tist(self):
        self.t_list = [26,33,10,18,45,6]#而且列表的长度也不是固定的
        self.starts = qthraed()#倒计时线程

        for i in range(len(self.t_list)):
            self.starts.myNum = self.t_list[i]
            self.starts.start()
            self.starts.sinOut.connect(self.List.append)
class qthraed(QThread):
    sinOut = pyqtSignal(str)

    def __init__(self, parent=None):
        super(qthraed, self).__init__(parent)
        self.Tist = True
        self.num = 0
        self.myNum = 1

    def __del__(self):
        self.Tist = False
        self.wait()

    def run(self):

        while self.myNum>= 0:
            self.sinOut.emit(str(self.myNum))
            self.myNum -= 1
            time.sleep(1)


if __name__ =="__main__":
    import  sys
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(w)
    w.show()
    sys.exit(app.exec_())

输出的结果是用列表的最后一个数字同时进行倒计时

我最终想要的结果是各自进行倒计时并且输出在UI界面上

  • 写回答

5条回答 默认 最新

  • chuifengde 2022-07-26 08:15
    关注

    要开多线程,这部分这样改:

    def tist(self):
            self.t_list = [26,33,10,18,45]#而且列表的长度也不是固定的
            self.starts = []
            for i in range(len(self.t_list)):            
                self.starts.append(qthraed())#倒计时线程
                self.starts[i].myNum = self.t_list[i]
                self.starts[i].start()
                self.starts[i].sinOut.connect(self.List.append)
    
    

    如果显示在多个控件中可以如下:

    #--*--coding:utf-8 --*--
    
    from PyQt5.QtCore import QThread,pyqtSignal
    from PyQt5 import QtCore,QtWidgets
    import time
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(553, 588)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.pushButton = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton.setGeometry(QtCore.QRect(470, 210, 75, 23))
            self.pushButton.setObjectName("pushButton")
    
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 553, 23))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.LL = []
            self.List = QtWidgets.QTextBrowser(self.centralwidget)
            self.List.setGeometry(20,20,50,300)
            self.List1 = QtWidgets.QTextBrowser(self.centralwidget)
            self.List1.setGeometry(100,20,50,300)
            self.List2 = QtWidgets.QTextBrowser(self.centralwidget)
            self.List2.setGeometry(200,20,50,300)        
            self.LL.append(self.List)
            self.LL.append(self.List1)
            self.LL.append(self.List2)
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
            self.pushButton.clicked.connect(lambda :self.tist())
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.pushButton.setText(_translate("MainWindow", "开始"))
    
        def tist(self):
            self.t_list = [26,33, 45]#而且列表的长度也不是固定的
            self.starts = []
            for i in range(len(self.t_list)):            
                self.starts.append(qthraed())#倒计时线程
                self.starts[i].myNum = self.t_list[i]
                self.starts[i].start()
                self.starts[i].sinOut.connect(self.LL[i].append)
    class qthraed(QThread):
        sinOut = pyqtSignal(str)
    
        def __init__(self, parent=None):
            super(qthraed, self).__init__(parent)
            self.Tist = True
            self.num = 0
            self.myNum = 1
    
        def __del__(self):
            self.Tist = False
            self.wait()
    
        def run(self):
    
            while self.myNum>= 0:
                self.sinOut.emit(str(self.myNum))
                self.myNum -= 1
                time.sleep(1)
    
    
    if __name__ =="__main__":
        import  sys
        app = QtWidgets.QApplication(sys.argv)
        w = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(w)
        w.show()
        sys.exit(app.exec_())
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 8月4日
  • 已采纳回答 7月27日
  • 创建了问题 7月25日

悬赏问题

  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。