PyQt5系列文章
效果图
完整代码
import sys
import time
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QPushButton,QHBoxLayout,QWidget,QApplication,QMainWindow
class QTimerDemo(QMainWindow):
def __init__(self):
super(QTimerDemo, self).__init__()
self.resize(400, 150)
self.setWindowTitle("QTimerDemo")
self.button1 = QPushButton("QTimer")
self.button1.clicked.connect(self.btnClick)
self.time = QTimer(self)
self.time.start(3000)
self.time.timeout.connect(self.timeoutEvent)
layout = QHBoxLayout()
layout.addWidget(self.button1)
mainFrame = QWidget()
mainFrame.setLayout(layout)
self.setCentralWidget(mainFrame)
def timeoutEvent(self):
print(time.strftime("%H:%M:%S", time.localtime()) + "> QTimer timeout!")
def btnClick(self):
print(time.strftime("%H:%M:%S", time.localtime()) + "> button clicked!")
self.button1.setEnabled(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QTimerDemo()
main.show()
sys.exit(app.exec_())