Python PyQt5 QTimer计数直到特定秒
我正在用python创建一个程序,我正在使用pyqt。我目前正在使用QTimer,我希望每秒钟打印一次“timer works”,并在5秒钟后停止打印。这是我的密码:Python PyQt5 QTimer计数直到特定秒,python,pyqt,pyqt5,qtimer,Python,Pyqt,Pyqt5,Qtimer,我正在用python创建一个程序,我正在使用pyqt。我目前正在使用QTimer,我希望每秒钟打印一次“timer works”,并在5秒钟后停止打印。这是我的密码: timers = [] def thread_func(): print("Thread works") timer = QtCore.QTimer() timer.timeout.connect(timer_func) timer.start(1000) print(timer.remai
timers = []
def thread_func():
print("Thread works")
timer = QtCore.QTimer()
timer.timeout.connect(timer_func)
timer.start(1000)
print(timer.remainingTime())
print(timer.isActive())
timers.append(timer)
def timer_func():
print("Timer works")
下面是一个简单的演示,展示了如何创建一个在固定超时次数后停止的计时器
from PyQt5 import QtCore
def start_timer(slot, count=1, interval=1000):
counter = 0
def handler():
nonlocal counter
counter += 1
slot(counter)
if counter >= count:
timer.stop()
timer.deleteLater()
timer = QtCore.QTimer()
timer.timeout.connect(handler)
timer.start(interval)
def timer_func(count):
print('Timer:', count)
if count >= 5:
QtCore.QCoreApplication.quit()
app = QtCore.QCoreApplication([])
start_timer(timer_func, 5)
app.exec_()
QTimer
类不支持在固定的超时次数后停止。你必须保持计数并明确停止。你能为我做一个演示吗?我只是个初学者。哇!这就是我一直在寻找的。谢谢你,朋友!