当前位置:   article > 正文

PyQt5 QTimer_pyqt qtimer安装

pyqt qtimer安装

PyQt5 QTimer


QTimer常用方法:

方法描述
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_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

在这里插入图片描述

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_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/854085
推荐阅读
相关标签
  

闽ICP备14008679号