PyQt5 QPushButton按钮
普通按钮(QPushButton)
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton from PyQt5.QtCore import QTimer class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.resize(250, 155) self.setWindowTitle('title') self.count = 30 self.button = QPushButton('获取验证码', self) self.button.setGeometry(50, 50, 100, 20) self.button.clicked.connect(self.acquire) self.time = QTimer(self) # 设置超时时间,默认值为0,单位毫秒 self.time.setInterval(1000) self.time.timeout.connect(self.refresh) self.show() def acquire(self): # 判断按钮是否被禁用 if self.button.isEnabled(): self.time.start() self.button.setEnabled(False) def refresh(self): if self.count > 0: self.button.setText(str(self.count) + '秒后重新获取') self.count -= 1 else: self.time.stop() self.button.setEnabled(True) self.button.setText('获取验证码') self.count = 30 if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())