透明的城市 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日

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器