赞
踩
方法 | 描述 |
---|---|
start(milliseconds) | 启动或重新启动定时器,时间间隔为毫秒。如果singleShot信号为真,定时器将仅被激活一次 |
stop() | 停止定时器 |
QTimer常用信号:
信号 | 描述 |
---|---|
singleShot | 在给定的时间间隔后调用一个槽函数时发射此信号 |
timeout | 当定时器超时时发射此信号 |
import sys from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt class WindowForm(QWidget): def __init__(self, parent=None): super(WindowForm, self).__init__(parent) self.setWindowTitle("QTimer Demo 1") self.listFile = QListWidget() self.label = QLabel("显示当前时间") self.startBtn = QPushButton("开始") self.stopBtn = QPushButton("停止") layout = QGridLayout(self) # 初始化一个定时器 self.timer = QTimer(self) self.timer.timeout.connect(self.showTime) layout.addWidget(self.label, 0,0, 1,2) layout.addWidget(self.startBtn, 1, 0) layout.addWidget(self.stopBtn, 1, 1) self.startBtn.clicked.connect(self.startTimer) self.stopBtn.clicked.connect(self.stopTimer) self.setLayout(layout) def showTime(self): time = QDateTime.currentDateTime() timeDisplay = time.toString("yyyy-MM-dd hh:mm:ss dddd") self.label.setText(timeDisplay) def startTimer(self): self.timer.start(1000) self.startBtn.setEnabled(False) self.stopBtn.setEnabled(True) def stopTimer(self): self.timer.stop() self.startBtn.setEnabled(True) self.stopBtn.setEnabled(False) if __name__ == "__main__": app =QApplication(sys.argv) win = WindowForm() win.show() sys.exit(app.exec_())
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
if __name__ == "__main__":
app = QApplication(sys.argv)
label = QLabel("<font color=red size=128><b>Hello PyQt, 窗口会在3秒后消失!</b></font>")
# 无边框窗口
label.setWindowFlags(Qt.SplashScreen | Qt.FramelessWindowHint)
label.show()
QTimer.singleShot(3000, app.quit)
sys.exit(app.exec_())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。