PyQt——QTimer

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
'''
    【简介】
    PyQT5中 QTimer例子
 
 
'''

from PyQt5.QtWidgets import QWidget,  QPushButton ,  QApplication ,QListWidget,  QGridLayout , QLabel
from PyQt5.QtCore import QTimer ,QDateTime
import sys

class WinForm(QWidget):  
   
    def __init__(self,parent=None):
        super(WinForm,self).__init__(parent)
        self.setWindowTitle("QTimer demo")
        self.listFile= QListWidget()
        self.label = QLabel('显示当前时间')
        self.startBtn = QPushButton('开始')
        self.endBtn = QPushButton('结束')
        layout = QGridLayout(self)

        # 初始化一个定时器
        self.timer = QTimer(self)
        # showTime()方法
        self.timer.timeout.connect(self.showTime)
       
        layout.addWidget(self.label,0,0,1,2)  
        layout.addWidget(self.startBtn,1,0)
        layout.addWidget(self.endBtn,1,1)      
       
        self.startBtn.clicked.connect( self.startTimer)
        self.endBtn.clicked.connect( self.endTimer)
               
        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.endBtn.setEnabled(True)

    def endTimer(self):
        self.timer.stop()
        self.startBtn.setEnabled(True)
        self.endBtn.setEnabled(False)
       
if __name__ == "__main__":  
    app = QApplication(sys.argv)  
    form = WinForm()  
    form.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
# -*- coding: utf-8 -*-
'''
    【简介】
    PyQT5中关闭应用例子
 
 
'''

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    label = QLabel("<font color=red size=128><b>Hello PyQT,窗口会在10秒后消失!</b></font>")
    label.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)
    label.show()

    # 设置10s后自动退出
    QTimer.singleShot(10000, app.quit)
    sys.exit(app.exec_())