PyQt5 QThread倒计时

创建多线程类
全局变量 sec
class work_thread(QThread):
timer = pyqtSignal() # 每隔一秒发送信号
end = pyqtSignal() # 计数完成发送信号

def run(self) -> None:
while True:
self.sleep(1)
if sec == 0:
self.end.emit() # 发送end信号
break
self.timer.emit()

实例化线程类,绑定计数,技术结束事件
self.label.setText(“20”)
global sec
sec = 20
self.work_thread = work_thread()
self.work_thread.timer.connect(self.count_time)
self.work_thread.end.connect(self.end)

绑定开始按钮事件
self.pushButton.clicked.connect(self.start)

事件方法
def count_time(self):
global sec
sec = int(self.label.text())
sec -= 1
self.label.setText(str(sec))

def end(self):
self.statusbar.showMessage(“计数停止”)

def start(self):
self.work_thread.start() # 启动线程