第九篇 -- 定时器和计时器

效果:

ui_timer.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui_timer.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(343, 229)
        self.groupBox = QtWidgets.QGroupBox(Form)
        self.groupBox.setGeometry(QtCore.QRect(20, 10, 301, 91))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.groupBox.setFont(font)
        self.groupBox.setObjectName("groupBox")
        self.btnStart = QtWidgets.QPushButton(self.groupBox)
        self.btnStart.setGeometry(QtCore.QRect(20, 20, 121, 23))
        font = QtGui.QFont()
        font.setBold(False)
        font.setWeight(50)
        self.btnStart.setFont(font)
        self.btnStart.setObjectName("btnStart")
        self.btnStop = QtWidgets.QPushButton(self.groupBox)
        self.btnStop.setGeometry(QtCore.QRect(160, 20, 121, 23))
        font = QtGui.QFont()
        font.setBold(False)
        font.setWeight(50)
        self.btnStop.setFont(font)
        self.btnStop.setObjectName("btnStop")
        self.btnSetIntv = QtWidgets.QPushButton(self.groupBox)
        self.btnSetIntv.setGeometry(QtCore.QRect(20, 60, 121, 23))
        font = QtGui.QFont()
        font.setBold(False)
        font.setWeight(50)
        self.btnSetIntv.setFont(font)
        self.btnSetIntv.setObjectName("btnSetIntv")
        self.spinBoxIntv = QtWidgets.QSpinBox(self.groupBox)
        self.spinBoxIntv.setGeometry(QtCore.QRect(160, 60, 121, 22))
        font = QtGui.QFont()
        font.setFamily("MS Shell Dlg 2")
        font.setBold(False)
        font.setWeight(50)
        self.spinBoxIntv.setFont(font)
        self.spinBoxIntv.setMaximum(2000)
        self.spinBoxIntv.setSingleStep(1)
        self.spinBoxIntv.setStepType(QtWidgets.QAbstractSpinBox.DefaultStepType)
        self.spinBoxIntv.setProperty("value", 1000)
        self.spinBoxIntv.setDisplayIntegerBase(10)
        self.spinBoxIntv.setObjectName("spinBoxIntv")
        self.groupBox_2 = QtWidgets.QGroupBox(Form)
        self.groupBox_2.setGeometry(QtCore.QRect(20, 110, 301, 81))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_2.setFont(font)
        self.groupBox_2.setObjectName("groupBox_2")
        self.LCDHour = QtWidgets.QLCDNumber(self.groupBox_2)
        self.LCDHour.setGeometry(QtCore.QRect(20, 20, 81, 51))
        self.LCDHour.setDigitCount(2)
        self.LCDHour.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.LCDHour.setObjectName("LCDHour")
        self.LCDMin = QtWidgets.QLCDNumber(self.groupBox_2)
        self.LCDMin.setGeometry(QtCore.QRect(110, 20, 81, 51))
        self.LCDMin.setDigitCount(2)
        self.LCDMin.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.LCDMin.setObjectName("LCDMin")
        self.LCDSec = QtWidgets.QLCDNumber(self.groupBox_2)
        self.LCDSec.setGeometry(QtCore.QRect(200, 20, 81, 51))
        self.LCDSec.setDigitCount(2)
        self.LCDSec.setSegmentStyle(QtWidgets.QLCDNumber.Flat)
        self.LCDSec.setObjectName("LCDSec")
        self.LabElapsedTime = QtWidgets.QLabel(Form)
        self.LabElapsedTime.setGeometry(QtCore.QRect(10, 200, 371, 16))
        self.LabElapsedTime.setObjectName("LabElapsedTime")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "定时器"))
        self.groupBox.setTitle(_translate("Form", "定时器"))
        self.btnStart.setText(_translate("Form", "开始"))
        self.btnStop.setText(_translate("Form", "停止"))
        self.btnSetIntv.setText(_translate("Form", "设置周期"))
        self.spinBoxIntv.setSuffix(_translate("Form", "  ms"))
        self.groupBox_2.setTitle(_translate("Form", "当前时间(小时:分:秒)"))
        self.LabElapsedTime.setText(_translate("Form", "     "))
View Code

myWidget_timer.py

#!/usr/bin/env python
# _*_ coding: UTF-8 _*_
"""=================================================
@Project -> File    : Operate-system -> myWidget_tiner.py
@IDE     : PyCharm
@Author  : Aimee
@Date    : 2020/4/10 9:25
@Desc    :
================================================="""

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QTime, QTimer

from ui_timer import Ui_Form


class QmyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.timer = QTimer()  # 创建定时器
        self.timer.stop()  # 停止
        self.timer.setInterval(1000)  # 定时周期1000ms
        self.timer.timeout.connect(self.do_timer_timeout)
        self.counter = QTime()  # 创建计时器

    def on_btnStart_clicked(self):
        self.timer.start()  # 开始定时
        self.counter.start()
        self.ui.btnStart.setEnabled(False)
        self.ui.btnStop.setEnabled(True)
        self.ui.btnSetIntv.setEnabled(False)

    def on_btnSetIntv_clicked(self):  # #设置定时器的周期
        self.timer.setInterval(self.ui.spinBoxIntv.value())

    def on_btnStop_clicked(self):
        self.timer.stop()  # 定时器停止
        tmMs = self.counter.elapsed()  # 计时器经过的毫秒数
        ms = tmMs % 1000  # 取余数,毫秒
        sec = tmMs/1000  # 整秒
        timeStr = "经过的时间:%d 秒,%d 毫秒" % (sec, ms)
        self.ui.LabElapsedTime.setText(timeStr)
        self.ui.btnStart.setEnabled(True)
        self.ui.btnStop.setEnabled(False)
        self.ui.btnSetIntv.setEnabled(True)

    def do_timer_timeout(self):  # 定时中断响应
        curTime = QTime.currentTime()  # 获取当前时间
        self.ui.LCDHour.display(curTime.hour())
        self.ui.LCDMin.display(curTime.minute())
        self.ui.LCDSec.display(curTime.second())


if __name__ == "__main__":
    app = QApplication(sys.argv)  # 创建app,用QApplication类
    form = QmyWidget()
    form.show()
    sys.exit(app.exec_())
View Code

PyQt5中的定时器类是QTimer。QTimer不是一个可见的界面组件,在Qt Designer的组件面板里找不到它。

QTimer主要的属性是interval,是定时中断的周期,单位是毫秒。

QTimer主要的信号是timeout(),在定时中断时发射此信号,若要在定时中断里作出响应,就需要编写与timeout()信号关联的槽函数。

功能分析:

(1)构造函数功能

在构造函数中创建了定时器对象self.timer并立刻停止。设置定时周期为1000ms,并为其timeout()信号关联了自定义槽函数do_timer_timeout()。

还创建了一个计时器对象self.counter,这是一个QTime类的实例,用于在开始与停止之间计算经过的时间。

(2)定时器开始运行

点击“开始”按钮后,定时器开始运行,计时器也开始运行。定时器的定时周期到了之后发射timeout()信号,触发关联的自定义槽函数do_timer_timeout()执行,此槽函数的功能通过类函数QTime.currentTime()读取当前时间,然后将时、分、秒显示在三个LCD组件上。

(3)定时器停止运行

点击“停止”按钮时,定时器停止运行。计时器通过调用elapsed()函数可以获得上次执行start()之后经过的毫秒数。

提示:计时器没有stop,每次start都是从0开始计时。

猜你喜欢

转载自www.cnblogs.com/smart-zihan/p/12677700.html
今日推荐