QTimer
import sys
from PyQt6.QtWidgets import QMainWindow, QApplication, QPushButton, QMessageBox
from PyQt6.QtCore import QTimer
class QWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(180,150)
self.initUI()
def initUI(self):
self.btn = QPushButton('发送验证码',self)
self.btn.clicked.connect(self.btnclicked)
# 创建计时器
self.time = QTimer(self)
# 设置计时器时间间隔(单位:ms;1s = 1000ms)
self.time.setInterval(1000)
# 计时器每经过一个时间间隔,便发出一次信号
self.time.timeout.connect(self.Refresh)
self.count = 10
self.resize(180,60)
self.setCentralWidget(self.btn)
self.show()
def btnclicked(self):
if self.btn.isEnabled():
# 使用计时器
self.time.start()
# 按钮不可用
self.btn.setEnabled(False)
def Refresh(self):
if self.count > 0:
self.btn.setText(str(self.count)+'秒后重发')
self.count -= 1
else:
self.time.stop()
# 按钮可用
self.btn.setEnabled(True)
self.btn.setText('发送验证码')
self.count = 10
def main():
app = QApplication(sys.argv)
qw = QWindow()
sys.exit(app.exec())
if __name__ == '__main__':
main()
QTimer Class | Qt Core 6.3.2